mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 15:26:29 +08:00
1 line
No EOL
935 KiB
JSON
1 line
No EOL
935 KiB
JSON
{"config":{"lang":["ja"],"separator":"[\\s\\-\uff0c\u3002]+","pipeline":["stemmer"]},"docs":[{"location":"","title":"Home","text":"<p> \u300a Hello\uff0c\u7b97\u6cd5 \u300b <p> \u52a8\u753b\u56fe\u89e3\u3001\u80fd\u8fd0\u884c\u3001\u53ef\u63d0\u95ee\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u5feb\u901f\u5165\u95e8\u6559\u7a0b </p> <p> </p> @Krahets </p> \u300c\u5168\u4e66\u52a8\u753b\u56fe\u89e3\u300d <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> \u300c\u4ee3\u7801\u4e00\u952e\u8fd0\u884c\u300d <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> \u300c\u53ef\u8ba8\u8bba\u4e0e\u63d0\u95ee\u300d <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> \u63a8\u8350\u8bed <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> <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> \u81f4\u8c22 <p>\u611f\u8c22\u672c\u5f00\u6e90\u4e66\u7684\u6bcf\u4e00\u4f4d\u64b0\u7a3f\u4eba\uff0c\u662f\u4ed6\u4eec\u7684\u65e0\u79c1\u5949\u732e\u8ba9\u8fd9\u672c\u4e66\u53d8\u5f97\u66f4\u597d\uff0c\u4ed6\u4eec\u662f\uff08\u987a\u5e8f\u7531 contrib.rocks \u81ea\u52a8\u751f\u6210\uff09\uff1a</p> <p> </p> <p>\u672c\u5f00\u6e90\u9879\u76ee\u7684\u7ef4\u62a4\u4e0e\u66f4\u65b0\u79bb\u4e0d\u5f00\u5404\u4f4d Reviewers \u7684\u8f9b\u52e4\u4ed8\u51fa\uff0c\u611f\u8c22\u4ed6\u4eec\uff08\u6309\u9996\u5b57\u6bcd\u987a\u5e8f\uff09\uff1a</p> justin-tseJS / TS krahetsJava / Python nuomi1Swift ReanonGo / C sjinzhRust / Zig \u4f5c\u8005 <p></p> <p> </p> \u9773\u5b87\u680b Krahets \u5927\u5382\u9ad8\u7ea7\u7b97\u6cd5\u5de5\u7a0b\u5e08 \u529b\u6263\uff08LeetCode\uff09\u5168\u7f51\u9605\u8bfb\u91cf\u6700\u9ad8\u535a\u4e3b LeetBook\u300a\u56fe\u89e3\u7b97\u6cd5\u6570\u636e\u7ed3\u6784\u300b\u5df2\u8ba2\u9605 22 \u4e07\u672c"},{"location":"chapter_array_and_linkedlist/array/","title":"4.1. \u00a0 \u6570\u7ec4\uff08Array\uff09","text":"<p>\u300c\u6570\u7ec4 Array\u300d\u662f\u4e00\u79cd\u5c06 \u76f8\u540c\u7c7b\u578b\u5143\u7d20 \u5b58\u50a8\u5728 \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u7684\u6570\u636e\u7ed3\u6784\uff0c\u5c06\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u79f0\u4e3a\u5143\u7d20\u7684\u300c\u7d22\u5f15 Index\u300d\u3002</p> <p></p> <p> Fig. \u6570\u7ec4\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f </p> <p>Note</p> <p>\u89c2\u5bdf\u4e0a\u56fe\uff0c\u6211\u4eec\u53d1\u73b0 \u6570\u7ec4\u9996\u5143\u7d20\u7684\u7d22\u5f15\u4e3a \\(0\\) \u3002\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u8fd9\u5e76\u4e0d\u7b26\u5408\u65e5\u5e38\u4e60\u60ef\uff0c\u9996\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u4e3a\u4ec0\u4e48\u4e0d\u662f \\(1\\) \u5462\uff0c\u8fd9\u4e0d\u662f\u66f4\u52a0\u81ea\u7136\u5417\uff1f\u6211\u8ba4\u540c\u4f60\u7684\u60f3\u6cd5\uff0c\u4f46\u8bf7\u5148\u8bb0\u4f4f\u8fd9\u4e2a\u8bbe\u5b9a\uff0c\u540e\u9762\u8bb2\u5185\u5b58\u5730\u5740\u8ba1\u7b97\u65f6\uff0c\u6211\u4f1a\u5c1d\u8bd5\u89e3\u7b54\u8fd9\u4e2a\u95ee\u9898\u3002</p> <p>\u6570\u7ec4\u521d\u59cb\u5316\u3002\u4e00\u822c\u4f1a\u7528\u5230\u65e0\u521d\u59cb\u503c\u3001\u7ed9\u5b9a\u521d\u59cb\u503c\u4e24\u79cd\u5199\u6cd5\uff0c\u53ef\u6839\u636e\u9700\u6c42\u9009\u53d6\u3002\u5728\u4e0d\u7ed9\u5b9a\u521d\u59cb\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u822c\u6240\u6709\u5143\u7d20\u4f1a\u88ab\u521d\u59cb\u5316\u4e3a\u9ed8\u8ba4\u503c \\(0\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array.java<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n</code></pre> array.cpp<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\n// \u5b58\u50a8\u5728\u6808\u4e0a\nint arr[5];\nint nums[5] { 1, 3, 2, 5, 4 };\n// \u5b58\u50a8\u5728\u5806\u4e0a\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n</code></pre> array.py<pre><code>\"\"\" \u521d\u59cb\u5316\u6570\u7ec4 \"\"\"\narr = [0] * 5 # [ 0, 0, 0, 0, 0 ]\nnums = [1, 3, 2, 5, 4] \n</code></pre> array.go<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr [5]int\n// \u5728 Go \u4e2d\uff0c\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[5]int\uff09\u4e3a\u6570\u7ec4\uff0c\u4e0d\u6307\u5b9a\u957f\u5ea6\u65f6\uff08[]int\uff09\u4e3a\u5207\u7247\n// \u7531\u4e8e Go \u7684\u6570\u7ec4\u88ab\u8bbe\u8ba1\u4e3a\u5728\u7f16\u8bd1\u671f\u786e\u5b9a\u957f\u5ea6\uff0c\u56e0\u6b64\u53ea\u80fd\u4f7f\u7528\u5e38\u91cf\u6765\u6307\u5b9a\u957f\u5ea6\n// \u4e3a\u4e86\u65b9\u4fbf\u5b9e\u73b0\u6269\u5bb9 extend() \u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5207\u7247\uff08Slice\uff09\u770b\u4f5c\u6570\u7ec4\uff08Array\uff09\nnums := []int{1, 3, 2, 5, 4}\n</code></pre> array.js<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n</code></pre> array.ts<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n</code></pre> array.c<pre><code>int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n</code></pre> array.cs<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n</code></pre> array.swift<pre><code>/* \u521d\u59cb\u5316\u6570\u7ec4 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n</code></pre> array.zig<pre><code>// \u521d\u59cb\u5316\u6570\u7ec4\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1. \u00a0 \u6570\u7ec4\u4f18\u70b9","text":"<p>\u5728\u6570\u7ec4\u4e2d\u8bbf\u95ee\u5143\u7d20\u975e\u5e38\u9ad8\u6548\u3002\u8fd9\u662f\u56e0\u4e3a\u5728\u6570\u7ec4\u4e2d\uff0c\u8ba1\u7b97\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u975e\u5e38\u5bb9\u6613\u3002\u7ed9\u5b9a\u6570\u7ec4\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u3001\u548c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5229\u7528\u4ee5\u4e0b\u516c\u5f0f\u53ef\u4ee5\u76f4\u63a5\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> Fig. \u6570\u7ec4\u5143\u7d20\u7684\u5185\u5b58\u5730\u5740\u8ba1\u7b97 </p> <pre><code># \u5143\u7d20\u5185\u5b58\u5730\u5740 = \u6570\u7ec4\u5185\u5b58\u5730\u5740 + \u5143\u7d20\u957f\u5ea6 * \u5143\u7d20\u7d22\u5f15\nelementAddr = firtstElementAddr + elementLength * elementIndex\n</code></pre> <p>\u4e3a\u4ec0\u4e48\u6570\u7ec4\u5143\u7d20\u7d22\u5f15\u4ece 0 \u5f00\u59cb\u7f16\u53f7\uff1f \u6839\u636e\u5730\u5740\u8ba1\u7b97\u516c\u5f0f\uff0c\u7d22\u5f15\u672c\u8d28\u4e0a\u8868\u793a\u7684\u662f\u5185\u5b58\u5730\u5740\u504f\u79fb\u91cf\uff0c\u9996\u4e2a\u5143\u7d20\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f \\(0\\) \uff0c\u90a3\u4e48\u7d22\u5f15\u662f \\(0\\) \u4e5f\u5c31\u5f88\u81ea\u7136\u4e86\u3002</p> <p>\u8bbf\u95ee\u5143\u7d20\u7684\u9ad8\u6548\u6027\u5e26\u6765\u4e86\u8bb8\u591a\u4fbf\u5229\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u968f\u673a\u83b7\u53d6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array.java<pre><code>/* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int[] nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = ThreadLocalRandom.current().\nnextInt(0, nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.cpp<pre><code>/* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int* nums, int size) {\n// \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = rand() % size;\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.py<pre><code>\"\"\" \u968f\u673a\u8bbf\u95ee\u5143\u7d20 \"\"\"\ndef random_access(nums):\n# \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandom_index = random.randint(0, len(nums) - 1)\n# \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandom_num = nums[random_index]\nreturn random_num\n</code></pre> array.go<pre><code>/* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nrandomIndex := rand.Intn(len(nums))\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nrandomNum = nums[randomIndex]\nreturn\n}\n</code></pre> array.js<pre><code>/* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunction randomAccess(nums) {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n</code></pre> array.ts<pre><code>/* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n// \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nconst random_index = Math.floor(Math.random() * nums.length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nconst random_num = nums[random_index];\nreturn random_num;\n}\n</code></pre> array.c<pre><code>[class]{}-[func]{randomAccess}\n</code></pre> array.cs<pre><code>/* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nint randomAccess(int[] nums)\n{\nRandom random = new();\n// \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nint randomIndex = random.Next(nums.Length);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nint randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre> array.swift<pre><code>/* \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n// \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\nlet randomIndex = nums.indices.randomElement()!\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nlet randomNum = nums[randomIndex]\nreturn randomNum\n}\n</code></pre> array.zig<pre><code>// \u968f\u673a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n// \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\nvar randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n// \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\nvar randomNum = nums[randomIndex];\nreturn randomNum;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2. \u00a0 \u6570\u7ec4\u7f3a\u70b9","text":"<p>\u6570\u7ec4\u5728\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u7531\u4e8e\u7cfb\u7edf\u65e0\u6cd5\u4fdd\u8bc1\u6570\u7ec4\u4e4b\u540e\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u53ef\u7528\u7684\uff0c\u56e0\u6b64\u6570\u7ec4\u957f\u5ea6\u65e0\u6cd5\u6269\u5c55\u3002\u800c\u82e5\u5e0c\u671b\u6269\u5bb9\u6570\u7ec4\uff0c\u5219\u9700\u65b0\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u628a\u539f\u6570\u7ec4\u5143\u7d20\u4f9d\u6b21\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\uff0c\u5728\u6570\u7ec4\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u662f\u975e\u5e38\u8017\u65f6\u7684\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array.java<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nint[] res = new int[nums.length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.cpp<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint* extend(int* nums, int size, int enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nint* res = new int[size + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\nres[i] = nums[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] nums;\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.py<pre><code>\"\"\" \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 \"\"\"\n# \u8bf7\u6ce8\u610f\uff0cPython \u7684 list \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n# \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 list \u770b\u4f5c\u662f\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\ndef extend(nums, enlarge):\n# \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nres = [0] * (len(nums) + enlarge)\n# \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i in range(len(nums)):\nres[i] = nums[i]\n# \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\n</code></pre> array.go<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nres := make([]int, len(nums)+enlarge)\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i, num := range nums {\nres[i] = num\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\n}\n</code></pre> array.js<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u662f\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nconst res = new Array(nums.length + enlarge).fill(0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.ts<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u662f\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nconst res = new Array(nums.length + enlarge).fill(0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.c<pre><code>[class]{}-[func]{extend}\n</code></pre> array.cs<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge)\n{\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nint[] res = new int[nums.Length + enlarge];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < nums.Length; i++)\n{\nres[i] = nums[i];\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> array.swift<pre><code>/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nvar res = Array(repeating: 0, count: nums.count + enlarge)\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor i in nums.indices {\nres[i] = nums[i]\n}\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res\n}\n</code></pre> array.zig<pre><code>// \u6269\u5c55\u6570\u7ec4\u957f\u5ea6\nfn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\nvar res = try mem_allocator.alloc(i32, nums.len + enlarge);\nstd.mem.set(i32, res, 0);\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(i32, res, nums);\n// \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\nreturn res;\n}\n</code></pre> <p>\u6570\u7ec4\u4e2d\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\u4e0b\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u6570\u7ec4\u4e2d\u95f4\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u7531\u4e8e\u6570\u7ec4\u5143\u7d20\u5728\u5185\u5b58\u4e2d\u662f\u201c\u7d27\u6328\u7740\u7684\u201d\uff0c\u5b83\u4eec\u4e4b\u95f4\u6ca1\u6709\u7a7a\u95f4\u518d\u653e\u4efb\u4f55\u6570\u636e\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5f97\u4e0d\u5c06\u6b64\u7d22\u5f15\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u7136\u540e\u518d\u628a\u5143\u7d20\u8d4b\u503c\u7ed9\u8be5\u7d22\u5f15\u3002</p> <p></p> <p> Fig. \u6570\u7ec4\u63d2\u5165\u5143\u7d20 </p> JavaC++PythonGoJavaScriptTypeScriptCC#Swift array.java<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.cpp<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int* nums, int size, int num, int index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int i = size - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.py<pre><code>\"\"\" \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num \"\"\"\ndef insert(nums, num, index):\n# \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i in range(len(nums) - 1, index, -1):\nnums[i] = nums[i - 1]\n# \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\n</code></pre> array.go<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i := len(nums) - 1; i > index; i-- {\nnums[i] = nums[i-1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\n}\n</code></pre> array.js<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.ts<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let i = nums.length - 1; i > index; i--) {\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.c<pre><code>[class]{}-[func]{insert}\n</code></pre> array.cs<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index)\n{\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int i = nums.Length - 1; i > index; i--)\n{\nnums[i] = nums[i - 1];\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num;\n}\n</code></pre> array.swift<pre><code>/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n// \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor i in sequence(first: nums.count - 1, next: { $0 > index + 1 ? $0 - 1 : nil }) {\nnums[i] = nums[i - 1]\n}\n// \u5c06 num \u8d4b\u7ed9 index \u5904\u5143\u7d20\nnums[index] = num\n}\n</code></pre> <p>\u5220\u9664\u5143\u7d20\u4e5f\u662f\u7c7b\u4f3c\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5220\u9664\u7d22\u5f15 \\(i\\) \u5904\u7684\u5143\u7d20\uff0c\u5219\u9700\u8981\u628a\u7d22\u5f15 \\(i\\) \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5220\u9664\u5143\u7d20\u540e\uff0c\u539f\u5148\u672b\u5c3e\u7684\u5143\u7d20\u53d8\u5f97\u201c\u65e0\u610f\u4e49\u201d\u4e86\uff0c\u6211\u4eec\u65e0\u9700\u7279\u610f\u53bb\u4fee\u6539\u5b83\u3002</p> <p></p> <p> Fig. \u6570\u7ec4\u5220\u9664\u5143\u7d20 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array.java<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.cpp<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(int* nums, int size, int index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int i = index; i < size - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.py<pre><code>\"\"\" \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 \"\"\"\ndef remove(nums, index):\n# \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in range(index, len(nums) - 1):\nnums[i] = nums[i + 1]\n</code></pre> array.go<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i := index; i < len(nums)-1; i++ {\nnums[i] = nums[i+1]\n}\n}\n</code></pre> array.js<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunction remove(nums, index) {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.ts<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let i = index; i < nums.length - 1; i++) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.c<pre><code>[class]{}-[func]{removeItem}\n</code></pre> array.cs<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nvoid remove(int[] nums, int index)\n{\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int i = index; i < nums.Length - 1; i++)\n{\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> array.swift<pre><code>/* \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\nlet count = nums.count\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor i in sequence(first: index, next: { $0 < count - 1 - 1 ? $0 + 1 : nil }) {\nnums[i] = nums[i + 1]\n}\n}\n</code></pre> array.zig<pre><code>// \u5220\u9664\u7d22\u5f15 index \u5904\u5143\u7d20\nfn remove(nums: []i32, index: usize) void {\n// \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nvar i = index;\nwhile (i < nums.len - 1) : (i += 1) {\nnums[i] = nums[i + 1];\n}\n}\n</code></pre> <p>\u603b\u7ed3\u6765\u770b\uff0c\u6570\u7ec4\u7684\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u6709\u4ee5\u4e0b\u7f3a\u70b9\uff1a</p> <ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6\u9ad8\uff1a\u6570\u7ec4\u7684\u63d2\u5165\u548c\u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3a \\(O(N)\\) \uff0c\u5176\u4e2d \\(N\\) \u4e3a\u6570\u7ec4\u957f\u5ea6\u3002</li> <li>\u4e22\u5931\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u5728\u63d2\u5165\u5143\u7d20\u540e\uff0c\u8d85\u51fa\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u7684\u5143\u7d20\u4f1a\u88ab\u4e22\u5931\u3002</li> <li>\u5185\u5b58\u6d6a\u8d39\uff1a\u6211\u4eec\u4e00\u822c\u4f1a\u521d\u59cb\u5316\u4e00\u4e2a\u6bd4\u8f83\u957f\u7684\u6570\u7ec4\uff0c\u53ea\u7528\u524d\u9762\u4e00\u90e8\u5206\uff0c\u8fd9\u6837\u5728\u63d2\u5165\u6570\u636e\u65f6\uff0c\u4e22\u5931\u7684\u672b\u5c3e\u5143\u7d20\u90fd\u662f\u6211\u4eec\u4e0d\u5173\u5fc3\u7684\uff0c\u4f46\u8fd9\u6837\u505a\u540c\u65f6\u4e5f\u4f1a\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002</li> </ul>"},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3. \u00a0 \u6570\u7ec4\u5e38\u7528\u64cd\u4f5c","text":"<p>\u6570\u7ec4\u904d\u5386\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u4e24\u79cd\u5e38\u7528\u7684\u904d\u5386\u65b9\u6cd5\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array.java<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (int num : nums) {\ncount++;\n}\n}\n</code></pre> array.cpp<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int* nums, int size) {\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < size; i++) {\ncount++;\n}\n}\n</code></pre> array.py<pre><code>\"\"\" \u904d\u5386\u6570\u7ec4 \"\"\"\ndef traverse(nums):\ncount = 0\n# \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i in range(len(nums)):\ncount += 1\n# \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor num in nums:\ncount += 1\n</code></pre> array.go<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\ncount := 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor i := 0; i < len(nums); i++ {\ncount++\n}\ncount = 0\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor range nums {\ncount++\n}\n}\n</code></pre> array.js<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let num of nums) {\ncount += 1;\n}\n}\n</code></pre> array.ts<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\nlet count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (let num of nums) {\ncount += 1;\n}\n}\n</code></pre> array.c<pre><code>[class]{}-[func]{traverse}\n</code></pre> array.cs<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums)\n{\nint count = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.Length; i++)\n{\ncount++;\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nforeach (int num in nums)\n{\ncount++;\n}\n}\n</code></pre> array.swift<pre><code>/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\nvar count = 0\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nfor _ in nums.indices {\ncount += 1\n}\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor _ in nums {\ncount += 1\n}\n}\n</code></pre> array.zig<pre><code>// \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\nvar count: i32 = 0;\n// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\nvar i: i32 = 0;\nwhile (i < nums.len) : (i += 1) {\ncount += 1;\n}\ncount = 0;\n// \u76f4\u63a5\u904d\u5386\u6570\u7ec4\nfor (nums) |_| {\ncount += 1;\n}\n}\n</code></pre> <p>\u6570\u7ec4\u67e5\u627e\u3002\u901a\u8fc7\u904d\u5386\u6570\u7ec4\uff0c\u67e5\u627e\u6570\u7ec4\u5185\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u5e76\u8f93\u51fa\u5bf9\u5e94\u7d22\u5f15\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array.java<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\nfor (int i = 0; i < nums.length; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> array.cpp<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int* nums, int size, int target) {\nfor (int i = 0; i < size; i++) {\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> array.py<pre><code>\"\"\" \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 \"\"\"\ndef find(nums, target):\nfor i in range(len(nums)):\nif nums[i] == target:\nreturn i\nreturn -1\n</code></pre> array.go<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\nindex = -1\nfor i := 0; i < len(nums); i++ {\nif nums[i] == target {\nindex = i\nbreak\n}\n}\nreturn\n}\n</code></pre> array.js<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\nfor (let i = 0; i < nums.length; i++) {\nif (nums[i] == target) return i;\n}\nreturn -1;\n}\n</code></pre> array.ts<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\nfor (let i = 0; i < nums.length; i++) {\nif (nums[i] === target) {\nreturn i;\n}\n}\nreturn -1;\n}\n</code></pre> array.c<pre><code>[class]{}-[func]{find}\n</code></pre> array.cs<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target)\n{\nfor (int i = 0; i < nums.Length; i++)\n{\nif (nums[i] == target)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> array.swift<pre><code>/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\nfor i in nums.indices {\nif nums[i] == target {\nreturn i\n}\n}\nreturn -1\n}\n</code></pre> array.zig<pre><code>// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\nfor (nums) |num, i| {\nif (num == target) return @intCast(i32, i);\n}\nreturn -1;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/array/#414","title":"4.1.4. \u00a0 \u6570\u7ec4\u5178\u578b\u5e94\u7528","text":"<p>\u968f\u673a\u8bbf\u95ee\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u968f\u673a\u62bd\u53d6\u4e00\u4e9b\u6837\u672c\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u6570\u7ec4\u5b58\u50a8\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u968f\u673a\u5e8f\u5217\uff0c\u6839\u636e\u7d22\u5f15\u5b9e\u73b0\u6837\u672c\u7684\u968f\u673a\u62bd\u53d6\u3002</p> <p>\u4e8c\u5206\u67e5\u627e\u3002\u4f8b\u5982\u524d\u6587\u67e5\u5b57\u5178\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u5178\u4e2d\u7684\u6240\u6709\u5b57\u6309\u7167\u62fc\u97f3\u987a\u5e8f\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\uff0c\u7136\u540e\u4f7f\u7528\u4e0e\u65e5\u5e38\u67e5\u7eb8\u8d28\u5b57\u5178\u76f8\u540c\u7684\u201c\u7ffb\u5f00\u4e2d\u95f4\uff0c\u6392\u9664\u4e00\u534a\u201d\u7684\u65b9\u5f0f\uff0c\u6765\u5b9e\u73b0\u4e00\u4e2a\u67e5\u7535\u5b50\u5b57\u5178\u7684\u7b97\u6cd5\u3002</p> <p>\u6df1\u5ea6\u5b66\u4e60\u3002\u795e\u7ecf\u7f51\u7edc\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u5411\u91cf\u3001\u77e9\u9635\u3001\u5f20\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u4ee5\u6570\u7ec4\u7684\u5f62\u5f0f\u6784\u5efa\u7684\u3002\u6570\u7ec4\u662f\u795e\u7ecf\u7f51\u7edc\u7f16\u7a0b\u4e2d\u6700\u5e38\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002</p>"},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2. \u00a0 \u94fe\u8868\uff08Linked List\uff09","text":"<p>\u5f15\u8a00</p> <p>\u5185\u5b58\u7a7a\u95f4\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u6392\u9664\u5df2\u5360\u7528\u7684\u5185\u5b58\uff0c\u7a7a\u95f2\u5185\u5b58\u5f80\u5f80\u662f\u6563\u843d\u5728\u5185\u5b58\u5404\u5904\u7684\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b58\u50a8\u6570\u7ec4\u9700\u8981\u5185\u5b58\u7a7a\u95f4\u8fde\u7eed\uff0c\u5f53\u6211\u4eec\u9700\u8981\u7533\u8bf7\u4e00\u4e2a\u5f88\u5927\u7684\u6570\u7ec4\u65f6\uff0c\u7cfb\u7edf\u4e0d\u4e00\u5b9a\u5b58\u5728\u8fd9\u4e48\u5927\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u3002\u800c\u94fe\u8868\u5219\u66f4\u52a0\u7075\u6d3b\uff0c\u4e0d\u9700\u8981\u5185\u5b58\u662f\u8fde\u7eed\u7684\uff0c\u53ea\u8981\u5269\u4f59\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u591f\u7528\u5373\u53ef\u3002</p> <p>\u300c\u94fe\u8868 Linked List\u300d\u662f\u4e00\u79cd\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u5355\u72ec\u7684\u5bf9\u8c61\uff0c\u5404\u4e2a\u5143\u7d20\uff08\u4e00\u822c\u79f0\u4e3a\u7ed3\u70b9\uff09\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\u8fde\u63a5\u3002\u7531\u4e8e\u7ed3\u70b9\u4e2d\u8bb0\u5f55\u4e86\u8fde\u63a5\u5173\u7cfb\uff0c\u56e0\u6b64\u94fe\u8868\u7684\u5b58\u50a8\u65b9\u5f0f\u76f8\u6bd4\u4e8e\u6570\u7ec4\u66f4\u52a0\u7075\u6d3b\uff0c\u7cfb\u7edf\u4e0d\u5fc5\u4fdd\u8bc1\u5185\u5b58\u5730\u5740\u7684\u8fde\u7eed\u6027\u3002</p> <p>\u94fe\u8868\u7684\u300c\u7ed3\u70b9 Node\u300d\u5305\u542b\u4e24\u9879\u6570\u636e\uff0c\u4e00\u662f\u7ed3\u70b9\u300c\u503c Value\u300d\uff0c\u4e8c\u662f\u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u300c\u6307\u9488 Pointer\u300d\uff08\u6216\u79f0\u300c\u5f15\u7528 Reference\u300d\uff09\u3002</p> <p></p> <p> Fig. \u94fe\u8868\u5b9a\u4e49\u4e0e\u5b58\u50a8\u65b9\u5f0f </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass ListNode {\nint val; // \u7ed3\u70b9\u503c\nListNode next; // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val; // \u7ed3\u70b9\u503c\nListNode *next; // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) : val(x), next(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n</code></pre> <pre><code>\"\"\" \u94fe\u8868\u7ed3\u70b9\u7c7b \"\"\" \nclass ListNode:\ndef __init__(self, x):\nself.val = x # \u7ed3\u70b9\u503c\nself.next = None # \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7ed3\u6784\u4f53 */\ntype ListNode struct {\nVal int // \u7ed3\u70b9\u503c\nNext *ListNode // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n// NewListNode \u6784\u9020\u51fd\u6570\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u94fe\u8868\nfunc NewListNode(val int) *ListNode {\nreturn &ListNode{\nVal: val,\nNext: nil,\n}\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7ed3\u6784\u4f53 */\nclass ListNode {\nval;\nnext;\nconstructor(val, next) {\nthis.val = (val === undefined ? 0 : val); // \u7ed3\u70b9\u503c\nthis.next = (next === undefined ? null : next); // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u5f15\u7528\n}\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7ed3\u6784\u4f53 */\nclass ListNode {\nval: number;\nnext: ListNode | null;\nconstructor(val?: number, next?: ListNode | null) {\nthis.val = val === undefined ? 0 : val; // \u7ed3\u70b9\u503c\nthis.next = next === undefined ? null : next; // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u5f15\u7528\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass ListNode\n{\nint val; // \u7ed3\u70b9\u503c\nListNode next; // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u5f15\u7528\nListNode(int x) => val = x; //\u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u7ed3\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n</code></pre> <pre><code>// \u94fe\u8868\u7ed3\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u7ed3\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\n}\n};\n}\n</code></pre> <p>\u5c3e\u7ed3\u70b9\u6307\u5411\u4ec0\u4e48\uff1f \u6211\u4eec\u4e00\u822c\u5c06\u94fe\u8868\u7684\u6700\u540e\u4e00\u4e2a\u7ed3\u70b9\u79f0\u4e3a\u300c\u5c3e\u7ed3\u70b9\u300d\uff0c\u5176\u6307\u5411\u7684\u662f\u300c\u7a7a\u300d\uff0c\u5728 Java / C++ / Python \u4e2d\u5206\u522b\u8bb0\u4e3a <code>null</code> / <code>nullptr</code> / <code>None</code> \u3002\u5728\u4e0d\u5f15\u8d77\u6b67\u4e49\u4e0b\uff0c\u672c\u4e66\u90fd\u4f7f\u7528 <code>null</code> \u6765\u8868\u793a\u7a7a\u3002</p> <p>\u94fe\u8868\u521d\u59cb\u5316\u65b9\u6cd5\u3002\u5efa\u7acb\u94fe\u8868\u5206\u4e3a\u4e24\u6b65\uff0c\u7b2c\u4e00\u6b65\u662f\u521d\u59cb\u5316\u5404\u4e2a\u7ed3\u70b9\u5bf9\u8c61\uff0c\u7b2c\u4e8c\u6b65\u662f\u6784\u5efa\u5f15\u7528\u6307\u5411\u5173\u7cfb\u3002\u5b8c\u6210\u540e\uff0c\u5373\u53ef\u4ee5\u4ece\u94fe\u8868\u7684\u9996\u4e2a\u7ed3\u70b9\uff08\u5373\u5934\u7ed3\u70b9\uff09\u51fa\u53d1\uff0c\u8bbf\u95ee\u5176\u4f59\u6240\u6709\u7684\u7ed3\u70b9\u3002</p> <p>Tip</p> <p>\u6211\u4eec\u901a\u5e38\u5c06\u5934\u7ed3\u70b9\u5f53\u4f5c\u94fe\u8868\u7684\u4ee3\u79f0\uff0c\u4f8b\u5982\u5934\u7ed3\u70b9 <code>head</code> \u548c\u94fe\u8868 <code>head</code> \u5b9e\u9645\u4e0a\u662f\u540c\u4e49\u7684\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linked_list.java<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\u70b9 \nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.cpp<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\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.py<pre><code>\"\"\" \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 \"\"\"\n# \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\u70b9 \nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.go<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\u70b9\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n</code></pre> linked_list.js<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\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\u7ed3\u70b9\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.c<pre><code>\n</code></pre> linked_list.cs<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\u70b9 \nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n</code></pre> linked_list.swift<pre><code>/* \u521d\u59cb\u5316\u94fe\u8868 1 -> 3 -> 2 -> 5 -> 4 */\n// \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\u70b9\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n</code></pre> linked_list.zig<pre><code>// \u521d\u59cb\u5316\u94fe\u8868\n// \u521d\u59cb\u5316\u5404\u4e2a\u7ed3\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>"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1. \u00a0 \u94fe\u8868\u4f18\u70b9","text":"<p>\u5728\u94fe\u8868\u4e2d\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9\u7684\u64cd\u4f5c\u6548\u7387\u9ad8\u3002\u6bd4\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5728\u94fe\u8868\u4e2d\u95f4\u7684\u4e24\u4e2a\u7ed3\u70b9 <code>A</code> , <code>B</code> \u4e4b\u95f4\u63d2\u5165\u4e00\u4e2a\u65b0\u7ed3\u70b9 <code>P</code> \uff0c\u6211\u4eec\u53ea\u9700\u8981\u6539\u53d8\u4e24\u4e2a\u7ed3\u70b9\u6307\u9488\u5373\u53ef\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u76f8\u6bd4\u6570\u7ec4\u7684\u63d2\u5165\u64cd\u4f5c\u9ad8\u6548\u5f88\u591a\u3002</p> <p></p> <p> Fig. \u94fe\u8868\u63d2\u5165\u7ed3\u70b9 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linked_list.java<pre><code>/* \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\nListNode n1 = n0.next;\nn0.next = P;\nP.next = n1;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P */\nvoid insert(ListNode* n0, ListNode* P) {\nListNode* n1 = n0->next;\nn0->next = P;\nP->next = n1;\n}\n</code></pre> linked_list.py<pre><code>\"\"\" \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P \"\"\"\ndef insert(n0, P):\nn1 = n0.next\nn0.next = P\nP.next = n1\n</code></pre> linked_list.go<pre><code>/* \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\nn1 := n0.Next\nn0.Next = P\nP.Next = n1\n}\n</code></pre> linked_list.js<pre><code>/* \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P */\nfunction insert(n0, P) {\nconst n1 = n0.next;\nn0.next = P;\nP.next = n1;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\nconst n1 = n0.next;\nn0.next = P;\nP.next = n1;\n}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{insertNode}\n</code></pre> linked_list.cs<pre><code>/* \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P */\nvoid insert(ListNode n0, ListNode P)\n{\nListNode? n1 = n0.next;\nn0.next = P;\nP.next = n1;\n}\n</code></pre> linked_list.swift<pre><code>/* \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\nlet n1 = n0.next\nn0.next = P\nP.next = n1\n}\n</code></pre> linked_list.zig<pre><code>// \u5728\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u7ed3\u70b9 P\nfn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {\nvar n1 = n0.?.next;\nn0.?.next = P;\nP.?.next = n1;\n}\n</code></pre> <p>\u5728\u94fe\u8868\u4e2d\u5220\u9664\u7ed3\u70b9\u4e5f\u5f88\u65b9\u4fbf\uff0c\u53ea\u9700\u8981\u6539\u53d8\u4e00\u4e2a\u7ed3\u70b9\u6307\u9488\u5373\u53ef\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u867d\u7136\u5728\u5b8c\u6210\u5220\u9664\u540e\u7ed3\u70b9 <code>P</code> \u4ecd\u7136\u6307\u5411 <code>n2</code> \uff0c\u4f46\u5b9e\u9645\u4e0a <code>P</code> \u5df2\u7ecf\u4e0d\u5c5e\u4e8e\u6b64\u94fe\u8868\u4e86\uff0c\u56e0\u4e3a\u904d\u5386\u6b64\u94fe\u8868\u662f\u65e0\u6cd5\u8bbf\u95ee\u5230 <code>P</code> \u7684\u3002</p> <p></p> <p> Fig. \u94fe\u8868\u5220\u9664\u7ed3\u70b9 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linked_list.java<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 */\nvoid remove(ListNode n0) {\nif (n0.next == null)\nreturn;\n// n0 -> P -> n1\nListNode P = n0.next;\nListNode n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 */\nvoid remove(ListNode* n0) {\nif (n0->next == nullptr)\nreturn;\n// n0 -> P -> n1\nListNode* P = n0->next;\nListNode* n1 = P->next;\nn0->next = n1;\n// \u91ca\u653e\u5185\u5b58\ndelete P;\n}\n</code></pre> linked_list.py<pre><code>\"\"\" \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 \"\"\"\ndef remove(n0):\nif not n0.next:\nreturn\n# n0 -> P -> n1\nP = n0.next\nn1 = P.next\nn0.next = n1\n</code></pre> linked_list.go<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunc removeNode(n0 *ListNode) {\nif n0.Next == nil {\nreturn\n}\n// n0 -> P -> n1\nP := n0.Next\nn1 := P.Next\nn0.Next = n1\n}\n</code></pre> linked_list.js<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunction remove(n0) {\nif (!n0.next)\nreturn;\n// n0 -> P -> n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunction remove(n0: ListNode): void {\nif (!n0.next) {\nreturn;\n}\n// n0 -> P -> n1\nconst P = n0.next;\nconst n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{removeNode}\n</code></pre> linked_list.cs<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 */\nvoid remove(ListNode n0)\n{\nif (n0.next == null)\nreturn;\n// n0 -> P -> n1\nListNode P = n0.next;\nListNode? n1 = P.next;\nn0.next = n1;\n}\n</code></pre> linked_list.swift<pre><code>/* \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunc remove(n0: ListNode) {\nif n0.next == nil {\nreturn\n}\n// n0 -> P -> n1\nlet P = n0.next\nlet n1 = P?.next\nn0.next = n1\nP?.next = nil\n}\n</code></pre> linked_list.zig<pre><code>// \u5220\u9664\u94fe\u8868\u7684\u7ed3\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u7ed3\u70b9\nfn remove(n0: ?*inc.ListNode(i32)) void {\nif (n0.?.next == null) return;\n// n0 -> P -> n1\nvar P = n0.?.next;\nvar n1 = P.?.next;\nn0.?.next = n1;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#422","title":"4.2.2. \u00a0 \u94fe\u8868\u7f3a\u70b9","text":"<p>\u94fe\u8868\u8bbf\u95ee\u7ed3\u70b9\u6548\u7387\u4f4e\u3002\u4e0a\u8282\u63d0\u5230\uff0c\u6570\u7ec4\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u8bbf\u95ee\u4efb\u610f\u5143\u7d20\uff0c\u4f46\u94fe\u8868\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u4efb\u610f\u7ed3\u70b9\u3002\u8fd9\u662f\u56e0\u4e3a\u8ba1\u7b97\u673a\u9700\u8981\u4ece\u5934\u7ed3\u70b9\u51fa\u53d1\uff0c\u4e00\u4e2a\u4e00\u4e2a\u5730\u5411\u540e\u904d\u5386\u5230\u76ee\u6807\u7ed3\u70b9\u3002\u4f8b\u5982\uff0c\u5018\u82e5\u60f3\u8981\u8bbf\u95ee\u94fe\u8868\u7d22\u5f15\u4e3a <code>index</code> \uff08\u5373\u7b2c <code>index + 1</code> \u4e2a\uff09\u7684\u7ed3\u70b9\uff0c\u90a3\u4e48\u9700\u8981 <code>index</code> \u6b21\u8bbf\u95ee\u64cd\u4f5c\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linked_list.java<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 */\nListNode access(ListNode head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 */\nListNode* access(ListNode* head, int index) {\nfor (int i = 0; i < index; i++) {\nif (head == nullptr)\nreturn nullptr;\nhead = head->next;\n}\nreturn head;\n}\n</code></pre> linked_list.py<pre><code>\"\"\" \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 \"\"\"\ndef access(head, index):\nfor _ in range(index):\nif not head:\nreturn None\nhead = head.next\nreturn head\n</code></pre> linked_list.go<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\nfor i := 0; i < index; i++ {\nif head == nil {\nreturn nil\n}\nhead = head.Next\n}\nreturn head\n}\n</code></pre> linked_list.js<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 */\nfunction access(head, index) {\nfor (let i = 0; i < index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.ts<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\nfor (let i = 0; i < index; i++) {\nif (!head) {\nreturn null;\n}\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{access}\n</code></pre> linked_list.cs<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 */\nListNode? access(ListNode head, int index)\n{\nfor (int i = 0; i < index; i++)\n{\nif (head == null)\nreturn null;\nhead = head.next;\n}\nreturn head;\n}\n</code></pre> linked_list.swift<pre><code>/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9 */\nfunc access(head: ListNode, index: Int) -> ListNode? {\nvar head: ListNode? = head\nfor _ in 0 ..< index {\nif head == nil {\nreturn nil\n}\nhead = head?.next\n}\nreturn head\n}\n</code></pre> linked_list.zig<pre><code>// \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u7ed3\u70b9\nfn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {\nvar head = node;\nvar i: i32 = 0;\nwhile (i < index) : (i += 1) {\nhead = head.?.next;\nif (head == null) return null;\n}\nreturn head;\n}\n</code></pre> <p>\u94fe\u8868\u7684\u5185\u5b58\u5360\u7528\u591a\u3002\u94fe\u8868\u4ee5\u7ed3\u70b9\u4e3a\u5355\u4f4d\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u9664\u4e86\u4fdd\u5b58\u503c\u5916\uff0c\u8fd8\u9700\u989d\u5916\u4fdd\u5b58\u6307\u9488\uff08\u5f15\u7528\uff09\u3002\u8fd9\u610f\u5473\u7740\u540c\u6837\u6570\u636e\u91cf\u4e0b\uff0c\u94fe\u8868\u6bd4\u6570\u7ec4\u9700\u8981\u5360\u7528\u66f4\u591a\u5185\u5b58\u7a7a\u95f4\u3002</p>"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3. \u00a0 \u94fe\u8868\u5e38\u7528\u64cd\u4f5c","text":"<p>\u904d\u5386\u94fe\u8868\u67e5\u627e\u3002\u904d\u5386\u94fe\u8868\uff0c\u67e5\u627e\u94fe\u8868\u5185\u503c\u4e3a <code>target</code> \u7684\u7ed3\u70b9\uff0c\u8f93\u51fa\u7ed3\u70b9\u5728\u94fe\u8868\u4e2d\u7684\u7d22\u5f15\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linked_list.java<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 */\nint find(ListNode head, int target) {\nint index = 0;\nwhile (head != null) {\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.cpp<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 */\nint find(ListNode* head, int target) {\nint index = 0;\nwhile (head != nullptr) {\nif (head->val == target)\nreturn index;\nhead = head->next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.py<pre><code>\"\"\" \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 \"\"\"\ndef find(head, target):\nindex = 0\nwhile head:\nif head.val == target:\nreturn index\nhead = head.next\nindex += 1\nreturn -1\n</code></pre> linked_list.go<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunc findNode(head *ListNode, target int) int {\nindex := 0\nfor head != nil {\nif head.Val == target {\nreturn index\n}\nhead = head.Next\nindex++\n}\nreturn -1\n}\n</code></pre> linked_list.js<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunction find(head, target) {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -1;\n}\n</code></pre> linked_list.ts<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\nlet index = 0;\nwhile (head !== null) {\nif (head.val === target) {\nreturn index;\n}\nhead = head.next;\nindex += 1;\n}\nreturn -1;\n}\n</code></pre> linked_list.c<pre><code>[class]{}-[func]{findNode}\n</code></pre> linked_list.cs<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 */\nint find(ListNode head, int target)\n{\nint index = 0;\nwhile (head != null)\n{\nif (head.val == target)\nreturn index;\nhead = head.next;\nindex++;\n}\nreturn -1;\n}\n</code></pre> linked_list.swift<pre><code>/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9 */\nfunc find(head: ListNode, target: Int) -> Int {\nvar head: ListNode? = head\nvar index = 0\nwhile head != nil {\nif head?.val == target {\nreturn index\n}\nhead = head?.next\nindex += 1\n}\nreturn -1\n}\n</code></pre> linked_list.zig<pre><code>// \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u7ed3\u70b9\nfn find(node: ?*inc.ListNode(i32), target: i32) i32 {\nvar head = node;\nvar index: i32 = 0;\nwhile (head != null) {\nif (head.?.val == target) return index;\nhead = head.?.next;\nindex += 1;\n}\nreturn -1;\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4. \u00a0 \u5e38\u89c1\u94fe\u8868\u7c7b\u578b","text":"<p>\u5355\u5411\u94fe\u8868\u3002\u5373\u4e0a\u8ff0\u4ecb\u7ecd\u7684\u666e\u901a\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u7684\u7ed3\u70b9\u6709\u300c\u503c\u300d\u548c\u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u300c\u6307\u9488\uff08\u5f15\u7528\uff09\u300d\u4e24\u9879\u6570\u636e\u3002\u6211\u4eec\u5c06\u9996\u4e2a\u7ed3\u70b9\u79f0\u4e3a\u5934\u7ed3\u70b9\uff0c\u5c3e\u7ed3\u70b9\u6307\u5411 <code>null</code> \u3002</p> <p>\u73af\u5f62\u94fe\u8868\u3002\u5982\u679c\u6211\u4eec\u4ee4\u5355\u5411\u94fe\u8868\u7684\u5c3e\u7ed3\u70b9\u6307\u5411\u5934\u7ed3\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\u6211\u4eec\u53ef\u4ee5\u5c06\u4efb\u610f\u7ed3\u70b9\u770b\u4f5c\u662f\u5934\u7ed3\u70b9\u3002</p> <p>\u53cc\u5411\u94fe\u8868\u3002\u5355\u5411\u94fe\u8868\u4ec5\u8bb0\u5f55\u4e86\u4e00\u4e2a\u65b9\u5411\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\uff0c\u5728\u53cc\u5411\u94fe\u8868\u7684\u7ed3\u70b9\u5b9a\u4e49\u4e2d\uff0c\u540c\u65f6\u6709\u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\uff08\u540e\u7ee7\u7ed3\u70b9\uff09\u548c\u4e0a\u4e00\u7ed3\u70b9\uff08\u524d\u9a71\u7ed3\u70b9\uff09\u7684\u300c\u6307\u9488\uff08\u5f15\u7528\uff09\u300d\u3002\u53cc\u5411\u94fe\u8868\u76f8\u5bf9\u4e8e\u5355\u5411\u94fe\u8868\u66f4\u52a0\u7075\u6d3b\uff0c\u5373\u53ef\u4ee5\u671d\u4e24\u4e2a\u65b9\u5411\u904d\u5386\u94fe\u8868\uff0c\u4f46\u4e5f\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass ListNode {\nint val; // \u7ed3\u70b9\u503c\nListNode next; // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev; // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) { val = x; } // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7ed3\u6784\u4f53 */\nstruct ListNode {\nint val; // \u7ed3\u70b9\u503c\nListNode *next; // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode *prev; // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // \u6784\u9020\u51fd\u6570\n};\n</code></pre> <pre><code>\"\"\" \u53cc\u5411\u94fe\u8868\u7ed3\u70b9\u7c7b \"\"\" \nclass ListNode:\ndef __init__(self, x):\nself.val = x # \u7ed3\u70b9\u503c\nself.next = None # \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nself.prev = None # \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9\u7ed3\u6784\u4f53 */\ntype DoublyListNode struct {\nVal int // \u7ed3\u70b9\u503c\nNext *DoublyListNode // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nPrev *DoublyListNode // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n// NewDoublyListNode \u521d\u59cb\u5316\nfunc NewDoublyListNode(val int) *DoublyListNode {\nreturn &DoublyListNode{\nVal: val,\nNext: nil,\nPrev: nil,\n}\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass ListNode {\nval;\nnext;\nprev;\nconstructor(val, next) {\nthis.val = val === undefined ? 0 : val; // \u7ed3\u70b9\u503c\nthis.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nthis.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\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; // \u7ed3\u70b9\u503c\nthis.next = next === undefined ? null : next; // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nthis.prev = prev === undefined ? null : prev; // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass ListNode {\nint val; // \u7ed3\u70b9\u503c\nListNode next; // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode prev; // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nListNode(int x) => val = x; // \u6784\u9020\u51fd\u6570\n}\n</code></pre> <pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass ListNode {\nvar val: Int // \u7ed3\u70b9\u503c\nvar next: ListNode? // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nvar prev: ListNode? // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\ninit(x: Int) { // \u6784\u9020\u51fd\u6570\nval = x\n}\n}\n</code></pre> <pre><code>// \u53cc\u5411\u94fe\u8868\u7ed3\u70b9\u7c7b\npub fn ListNode(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nval: T = 0, // \u7ed3\u70b9\u503c\nnext: ?*Self = null, // \u6307\u5411\u540e\u7ee7\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\nprev: ?*Self = null, // \u6307\u5411\u524d\u9a71\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n// \u6784\u9020\u51fd\u6570\npub fn init(self: *Self, x: i32) void {\nself.val = x;\nself.next = null;\nself.prev = null;\n}\n};\n}\n</code></pre> <p></p> <p> Fig. \u5e38\u89c1\u94fe\u8868\u79cd\u7c7b </p>"},{"location":"chapter_array_and_linkedlist/list/","title":"4.3. \u00a0 \u5217\u8868\uff08List\uff09","text":"<p>\u7531\u4e8e\u957f\u5ea6\u4e0d\u53ef\u53d8\uff0c\u6570\u7ec4\u7684\u5b9e\u7528\u6027\u5927\u5927\u964d\u4f4e\u3002\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e8b\u5148\u5e76\u4e0d\u77e5\u9053\u4f1a\u8f93\u5165\u591a\u5c11\u6570\u636e\uff0c\u8fd9\u5c31\u4e3a\u6570\u7ec4\u957f\u5ea6\u7684\u9009\u62e9\u5e26\u6765\u4e86\u5f88\u5927\u56f0\u96be\u3002\u957f\u5ea6\u9009\u5c0f\u4e86\uff0c\u9700\u8981\u5728\u6dfb\u52a0\u6570\u636e\u4e2d\u9891\u7e41\u5730\u6269\u5bb9\u6570\u7ec4\uff1b\u957f\u5ea6\u9009\u5927\u4e86\uff0c\u53c8\u9020\u6210\u5185\u5b58\u7a7a\u95f4\u7684\u6d6a\u8d39\u3002</p> <p>\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8bde\u751f\u4e86\u4e00\u79cd\u88ab\u79f0\u4e3a\u300c\u5217\u8868 List\u300d\u7684\u6570\u636e\u7ed3\u6784\u3002\u5217\u8868\u53ef\u4ee5\u88ab\u7406\u89e3\u4e3a\u957f\u5ea6\u53ef\u53d8\u7684\u6570\u7ec4\uff0c\u56e0\u6b64\u4e5f\u5e38\u88ab\u79f0\u4e3a\u300c\u52a8\u6001\u6570\u7ec4 Dynamic Array\u300d\u3002\u5217\u8868\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\uff0c\u7ee7\u627f\u4e86\u6570\u7ec4\u7684\u4f18\u70b9\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u5b9e\u65f6\u6269\u5bb9\u3002\u5728\u5217\u8868\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u5730\u6dfb\u52a0\u5143\u7d20\uff0c\u800c\u4e0d\u7528\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":"<p>\u521d\u59cb\u5316\u5217\u8868\u3002\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u5230\u201c\u65e0\u521d\u59cb\u503c\u201d\u548c\u201c\u6709\u521d\u59cb\u503c\u201d\u7684\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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.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.py<pre><code>\"\"\" \u521d\u59cb\u5316\u5217\u8868 \"\"\"\n# \u65e0\u521d\u59cb\u503c\nlist1 = []\n# \u6709\u521d\u59cb\u503c\nlist = [1, 3, 2, 5, 4]\n</code></pre> list_test.go<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlist1 := []int\n// \u6709\u521d\u59cb\u503c\nlist := []int{1, 3, 2, 5, 4}\n</code></pre> list.js<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1 = [];\n// \u6709\u521d\u59cb\u503c\nconst list = [1, 3, 2, 5, 4];\n</code></pre> list.ts<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nconst list1: number[] = [];\n// \u6709\u521d\u59cb\u503c\nconst list: number[] = [1, 3, 2, 5, 4];\n</code></pre> list.c<pre><code>\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.swift<pre><code>/* \u521d\u59cb\u5316\u5217\u8868 */\n// \u65e0\u521d\u59cb\u503c\nlet list1: [Int] = []\n// \u6709\u521d\u59cb\u503c\nvar list = [1, 3, 2, 5, 4]\n</code></pre> list.zig<pre><code>// \u521d\u59cb\u5316\u5217\u8868\nvar list = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list.deinit();\ntry list.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n</code></pre> <p>\u8bbf\u95ee\u4e0e\u66f4\u65b0\u5143\u7d20\u3002\u5217\u8868\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u662f\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u5185\u8bbf\u95ee\u4e0e\u66f4\u65b0\u5143\u7d20\uff0c\u6548\u7387\u5f88\u9ad8\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig list.java<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nint num = list.get(1); // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist.set(1, 0); // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.cpp<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.py<pre><code>\"\"\" \u8bbf\u95ee\u5143\u7d20 \"\"\"\nnum = list[1] # \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n\"\"\" \u66f4\u65b0\u5143\u7d20 \"\"\"\nlist[1] = 0 # \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list_test.go<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nnum := list[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.js<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nconst num = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.ts<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nconst num: number = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.c<pre><code>\n</code></pre> list.cs<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nint num = list[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.swift<pre><code>/* \u8bbf\u95ee\u5143\u7d20 */\nlet num = list[1] // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n/* \u66f4\u65b0\u5143\u7d20 */\nlist[1] = 0 // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0\n</code></pre> list.zig<pre><code>// \u8bbf\u95ee\u5143\u7d20\nvar num = list.items[1]; // \u8bbf\u95ee\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\n// \u66f4\u65b0\u5143\u7d20\nlist.items[1] = 0; // \u5c06\u7d22\u5f15 1 \u5904\u7684\u5143\u7d20\u66f4\u65b0\u4e3a 0 \n</code></pre> <p>\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u3001\u63d2\u5165\u3001\u5220\u9664\u5143\u7d20\u3002\u76f8\u5bf9\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\u662f\u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20\u7684\u6548\u7387\u4ecd\u4e0e\u6570\u7ec4\u4e00\u6837\u4f4e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(N)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig list.java<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.add(1);\nlist.add(3);\nlist.add(2);\nlist.add(5);\nlist.add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.add(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.cpp<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push_back(1);\nlist.push_back(3);\nlist.push_back(2);\nlist.push_back(5);\nlist.push_back(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(list.begin() + 3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.erase(list.begin() + 3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.py<pre><code>\"\"\" \u6e05\u7a7a\u5217\u8868 \"\"\"\nlist.clear()\n\"\"\" \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 \"\"\"\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n\"\"\" \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 \"\"\"\nlist.insert(3, 6) # \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n\"\"\" \u5220\u9664\u5143\u7d20 \"\"\"\nlist.pop(3) # \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list_test.go<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist = nil\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist = append(list, 1)\nlist = append(list, 3)\nlist = append(list, 2)\nlist = append(list, 5)\nlist = append(list, 4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist = append(list[:3], append([]int{6}, list[3:]...)...) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist = append(list[:3], list[4:]...) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.js<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n</code></pre> list.ts<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.length = 0;\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.push(1);\nlist.push(3);\nlist.push(2);\nlist.push(5);\nlist.push(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.splice(3, 0, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.splice(3, 1);\n</code></pre> list.c<pre><code>\n</code></pre> list.cs<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.Clear();\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.Add(1);\nlist.Add(3);\nlist.Add(2);\nlist.Add(5);\nlist.Add(4);\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.Insert(3, 6);\n/* \u5220\u9664\u5143\u7d20 */\nlist.RemoveAt(3);\n</code></pre> list.swift<pre><code>/* \u6e05\u7a7a\u5217\u8868 */\nlist.removeAll()\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nlist.append(1)\nlist.append(3)\nlist.append(2)\nlist.append(5)\nlist.append(4)\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nlist.insert(6, at: 3) // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n/* \u5220\u9664\u5143\u7d20 */\nlist.remove(at: 3) // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> list.zig<pre><code>// \u6e05\u7a7a\u5217\u8868\nlist.clearRetainingCapacity();\n// \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\ntry list.append(1);\ntry list.append(3);\ntry list.append(2);\ntry list.append(5);\ntry list.append(4);\n// \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\ntry list.insert(3, 6); // \u5728\u7d22\u5f15 3 \u5904\u63d2\u5165\u6570\u5b57 6\n// \u5220\u9664\u5143\u7d20\n_ = list.orderedRemove(3); // \u5220\u9664\u7d22\u5f15 3 \u5904\u7684\u5143\u7d20\n</code></pre> <p>\u904d\u5386\u5217\u8868\u3002\u4e0e\u6570\u7ec4\u4e00\u6837\uff0c\u5217\u8868\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u904d\u5386\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>for-each</code> \u76f4\u63a5\u904d\u5386\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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.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.py<pre><code>\"\"\" \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 \"\"\"\ncount = 0\nfor i in range(len(list)):\ncount += 1\n\"\"\" \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 \"\"\"\ncount = 0\nfor n in list:\ncount += 1\n</code></pre> list_test.go<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\ncount := 0\nfor i := 0; i < len(list); i++ {\ncount++\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor range list {\ncount++\n}\n</code></pre> list.js<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nlet count = 0;\nfor (let i = 0; i < 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.c<pre><code>\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++)\n{\ncount++;\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0;\nforeach (int n in list)\n{\ncount++;\n}\n</code></pre> list.swift<pre><code>/* \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868 */\nvar count = 0\nfor _ in list.indices {\ncount += 1\n}\n/* \u76f4\u63a5\u904d\u5386\u5217\u8868\u5143\u7d20 */\ncount = 0\nfor _ in list {\ncount += 1\n}\n</code></pre> list.zig<pre><code>// \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u5217\u8868\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < 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> <p>\u62fc\u63a5\u4e24\u4e2a\u5217\u8868\u3002\u518d\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868 <code>list1</code> \uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u4e2d\u4e00\u4e2a\u5217\u8868\u62fc\u63a5\u5230\u53e6\u4e00\u4e2a\u7684\u5c3e\u90e8\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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.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.py<pre><code>\"\"\" \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 \"\"\"\nlist1 = [6, 8, 7, 10, 9]\nlist += list1 # \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list_test.go<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlist1 := []int{6, 8, 7, 10, 9}\nlist = append(list, list1...) // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.js<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1 = [6, 8, 7, 10, 9];\nlist.push(...list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.ts<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nconst list1: number[] = [6, 8, 7, 10, 9];\nlist.push(...list1); // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.c<pre><code>\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.swift<pre><code>/* \u62fc\u63a5\u4e24\u4e2a\u5217\u8868 */\nlet list1 = [6, 8, 7, 10, 9]\nlist.append(contentsOf: list1) // \u5c06\u5217\u8868 list1 \u62fc\u63a5\u5230 list \u4e4b\u540e\n</code></pre> list.zig<pre><code>// \u62fc\u63a5\u4e24\u4e2a\u5217\u8868\nvar list1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer list1.deinit();\ntry list1.appendSlice(&[_]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> <p>\u6392\u5e8f\u5217\u8868\u3002\u6392\u5e8f\u4e5f\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b8c\u6210\u5217\u8868\u6392\u5e8f\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u5728\u6570\u7ec4\u7c7b\u7b97\u6cd5\u9898\u4e2d\u7ecf\u5e38\u8003\u5bdf\u7684\u300c\u4e8c\u5206\u67e5\u627e\u300d\u548c\u300c\u53cc\u6307\u9488\u300d\u7b97\u6cd5\u4e86\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig list.java<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nCollections.sort(list); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.cpp<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nsort(list.begin(), list.end()); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.py<pre><code>\"\"\" \u6392\u5e8f\u5217\u8868 \"\"\"\nlist.sort() # \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list_test.go<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nsort.Ints(list) // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.js<pre><code>/* \u6392\u5e8f\u5217\u8868 */ list.sort((a, b) => 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.c<pre><code>\n</code></pre> list.cs<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.Sort(); // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.swift<pre><code>/* \u6392\u5e8f\u5217\u8868 */\nlist.sort() // \u6392\u5e8f\u540e\uff0c\u5217\u8868\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\n</code></pre> list.zig<pre><code>// \u6392\u5e8f\u5217\u8868\nstd.sort.sort(i32, list.items, {}, comptime std.sort.asc(i32));\n</code></pre>"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2. \u00a0 \u5217\u8868\u7b80\u6613\u5b9e\u73b0 *","text":"<p>\u4e3a\u4e86\u5e2e\u52a9\u52a0\u6df1\u5bf9\u5217\u8868\u7684\u7406\u89e3\uff0c\u6211\u4eec\u5728\u6b64\u63d0\u4f9b\u4e00\u4e2a\u5217\u8868\u7684\u7b80\u6613\u7248\u672c\u7684\u5b9e\u73b0\u3002\u9700\u8981\u5173\u6ce8\u4e09\u4e2a\u6838\u5fc3\u70b9\uff1a</p> <ul> <li>\u521d\u59cb\u5bb9\u91cf\uff1a\u9009\u53d6\u4e00\u4e2a\u5408\u7406\u7684\u6570\u7ec4\u7684\u521d\u59cb\u5bb9\u91cf <code>initialCapacity</code> \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\u9700\u8981\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf <code>size</code> \uff0c\u7528\u6765\u8bb0\u5f55\u5217\u8868\u5f53\u524d\u6709\u591a\u5c11\u4e2a\u5143\u7d20\uff0c\u5e76\u968f\u7740\u5143\u7d20\u63d2\u5165\u4e0e\u5220\u9664\u5b9e\u65f6\u66f4\u65b0\u3002\u6839\u636e\u6b64\u53d8\u91cf\uff0c\u53ef\u4ee5\u5b9a\u4f4d\u5217\u8868\u7684\u5c3e\u90e8\uff0c\u4ee5\u53ca\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9\u3002</li> <li>\u6269\u5bb9\u673a\u5236\uff1a\u63d2\u5165\u5143\u7d20\u6709\u53ef\u80fd\u5bfc\u81f4\u8d85\u51fa\u5217\u8868\u5bb9\u91cf\uff0c\u6b64\u65f6\u9700\u8981\u6269\u5bb9\u5217\u8868\uff0c\u65b9\u6cd5\u662f\u5efa\u7acb\u4e00\u4e2a\u66f4\u5927\u7684\u6570\u7ec4\u6765\u66ff\u6362\u5f53\u524d\u6570\u7ec4\u3002\u9700\u8981\u7ed9\u5b9a\u4e00\u4e2a\u6269\u5bb9\u500d\u6570 <code>extendRatio</code> \uff0c\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> <p>\u672c\u793a\u4f8b\u662f\u4e3a\u4e86\u5e2e\u52a9\u8bfb\u8005\u5bf9\u5982\u4f55\u5b9e\u73b0\u5217\u8868\u4ea7\u751f\u76f4\u89c2\u7684\u8ba4\u8bc6\u3002\u5b9e\u9645\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u5217\u8868\u7684\u5b9e\u73b0\u8fdc\u6bd4\u4ee5\u4e0b\u4ee3\u7801\u590d\u6742\u4e14\u6807\u51c6\uff0c\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u67e5\u9605\u6e90\u7801\u5b66\u4e60\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig my_list.java<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate int[] nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int capacity = 10; // \u5217\u8868\u5bb9\u91cf\nprivate int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList() {\nnums = new int[capacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic int size() {\nreturn size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity() {\nreturn capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\nnums[size] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size == capacity())\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index) {\nif (index < 0 || index >= size)\nthrow new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < size - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nsize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic void extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nnums = Arrays.copyOf(nums, capacity() * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\ncapacity = nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray() {\nint size = size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[size];\nfor (int i = 0; i < size; i++) {\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.cpp<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate:\nint* nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nint numsCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nint numsSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nint extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nMyList() {\nnums = new int[numsCapacity];\n}\n/* \u6790\u6784\u65b9\u6cd5 */\n~MyList() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nint size() {\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity() {\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(int index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(int index, int num) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(int num) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\nnums[size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(int index, int num) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (size() == capacity())\nextendCapacity();\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = size() - 1; j >= index; j--) {\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nint remove(int index) {\nif (index < 0 || index >= size())\nthrow out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < size() - 1; j++) {\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nint newCapacity = capacity() * extendRatio;\nint* tmp = nums;\nnums = new int[newCapacity];\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nfor (int i = 0; i < size(); i++) {\nnums[i] = tmp[i];\n}\n// \u91ca\u653e\u5185\u5b58\ndelete[] tmp;\nnumsCapacity = newCapacity;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> vec(size());\nfor (int i = 0; i < size(); i++) {\nvec[i] = nums[i];\n}\nreturn vec;\n}\n};\n</code></pre> my_list.py<pre><code>\"\"\" \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 \"\"\"\nclass MyList:\n\"\"\" \u6784\u9020\u65b9\u6cd5 \"\"\"\ndef __init__(self):\nself.__capacity = 10 # \u5217\u8868\u5bb9\u91cf\nself.__nums = [0] * self.__capacity # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nself.__size = 0 # \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nself.__extend_ratio = 2 # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\"\"\" \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 \"\"\"\ndef size(self):\nreturn self.__size\n\"\"\" \u83b7\u53d6\u5217\u8868\u5bb9\u91cf \"\"\"\ndef capacity(self):\nreturn self.__capacity\n\"\"\" \u8bbf\u95ee\u5143\u7d20 \"\"\"\ndef get(self, index):\n# \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nassert index >= 0 and index < self.__size, \"\u7d22\u5f15\u8d8a\u754c\"\nreturn self.__nums[index]\n\"\"\" \u66f4\u65b0\u5143\u7d20 \"\"\"\ndef set(self, num, index):\nassert index >= 0 and index < self.__size, \"\u7d22\u5f15\u8d8a\u754c\"\nself.__nums[index] = num\n\"\"\" \u4e2d\u95f4\u63d2\u5165\uff08\u5c3e\u90e8\u6dfb\u52a0\uff09\u5143\u7d20 \"\"\"\ndef add(self, num, index=-1):\nassert index >= 0 and index < self.__size, \"\u7d22\u5f15\u8d8a\u754c\"\n# \u82e5\u4e0d\u6307\u5b9a\u7d22\u5f15 index \uff0c\u5219\u5411\u6570\u7ec4\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\nif index == -1:\nindex = self.__size\n# \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif self.__size == self.capacity():\nself.extend_capacity()\n# \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in range(self.__size - 1, index - 1, -1):\nself.__nums[j + 1] = self.__nums[j]\nself.__nums[index] = num\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size += 1\n\"\"\" \u5220\u9664\u5143\u7d20 \"\"\"\ndef remove(self, index):\nassert index >= 0 and index < self.__size, \"\u7d22\u5f15\u8d8a\u754c\"\nnum = self.nums[index]\n# \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in range(index, self.__size - 1):\nself.__nums[j] = self.__nums[j + 1]\n# \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.__size -= 1\n# \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\n\"\"\" \u5217\u8868\u6269\u5bb9 \"\"\"\ndef extend_capacity(self):\n# \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a self.__size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nself.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)\n# \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.__capacity = len(self.__nums)\n\"\"\" \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 \"\"\"\ndef to_array(self):\nreturn self.__nums[:self.__size]\n</code></pre> my_list.go<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\ntype myList struct {\nnumsCapacity int\nnums []int\nnumsSize int\nextendRatio int\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newMyList() *myList {\nreturn &myList{\nnumsCapacity: 10, // \u5217\u8868\u5bb9\u91cf\nnums: make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnumsSize: 0, // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio: 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\nreturn l.numsSize\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\nreturn l.numsCapacity\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn l.nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nl.nums[index] = num\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\nl.nums[l.numsSize] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif l.numsSize == l.numsCapacity {\nl.extendCapacity()\n}\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j := l.numsSize - 1; j >= index; j-- {\nl.nums[j+1] = l.nums[j]\n}\nl.nums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize++\n}\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\nif index < 0 || index >= l.numsSize {\npanic(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnum := l.nums[index]\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j := index; j < l.numsSize-1; j++ {\nl.nums[j] = l.nums[j+1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nl.numsSize--\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\n}\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a self.__size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nl.nums = append(l.nums, make([]int, l.numsCapacity*(l.extendRatio-1))...)\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nl.numsCapacity = len(l.nums)\n}\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nreturn l.nums[:l.numsSize]\n}\n</code></pre> my_list.js<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\n#nums = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n#capacity = 10; // \u5217\u8868\u5bb9\u91cf\n#size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n#extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.#nums = new Array(this.#capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nsize() {\nreturn this.#size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\ncapacity() {\nreturn this.#capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nget(index) {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= this.#size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.#nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nset(index, num) {\nif (index < 0 || index >= this.#size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.#nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nadd(num) {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.#nums[this.#size] = num;\nthis.#size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\ninsert(index, num) {\nif (index < 0 || index >= this.#size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (this.#size === this.#capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this.#size - 1; j >= index; j--) {\nthis.#nums[j + 1] = this.#nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#nums[index] = num;\nthis.#size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\nremove(index) {\nif (index < 0 || index >= this.#size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.#nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j < this.#size - 1; j++) {\nthis.#nums[j] = this.#nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.#size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\nextendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nthis.#nums = this.#nums.concat(\nnew Array(this.capacity() * (this.#extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis.#capacity = this.#nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\ntoArray() {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst nums = new Array(size);\nfor (let i = 0; i < size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.ts<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate nums: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\nprivate _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor() {\nthis.nums = new Array(this._capacity);\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic size(): number {\nreturn this._size;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic capacity(): number {\nreturn this._capacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic get(index: number): number {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= this._size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\nreturn this.nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic set(index: number, num: number): void {\nif (index < 0 || index >= this._size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\nthis.nums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic add(num: number): void {\n// \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\nif (this._size === this._capacity)\nthis.extendCapacity();\n// \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\nthis.nums[this._size] = num;\nthis._size++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic insert(index: number, num: number): void {\nif (index < 0 || index >= this._size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (this._size === this._capacity) {\nthis.extendCapacity();\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (let j = this._size - 1; j >= index; j--) {\nthis.nums[j + 1] = this.nums[j];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis.nums[index] = num;\nthis._size++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic remove(index: number): number {\nif (index < 0 || index >= this._size)\nthrow new Error('\u7d22\u5f15\u8d8a\u754c');\nlet num = this.nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (let j = index; j < this._size - 1; j++) {\nthis.nums[j] = this.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nthis._size--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic extendCapacity(): void {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nthis.nums = this.nums.concat(\nnew Array(this.capacity() * (this.extendRatio - 1))\n);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nthis._capacity = this.nums.length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic toArray(): number[] {\nlet size = this.size();\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nlet nums = new Array(size);\nfor (let i = 0; i < size; i++) {\nnums[i] = this.get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.c<pre><code>[class]{myList}-[func]{}\n</code></pre> my_list.cs<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList\n{\nprivate int[] nums; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate int numsCapacity = 10; // \u5217\u8868\u5bb9\u91cf\nprivate int numsSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\npublic MyList()\n{\nnums = new int[numsCapacity];\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\npublic int size()\n{\nreturn numsSize;\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\npublic int capacity()\n{\nreturn numsCapacity;\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\npublic int get(int index)\n{\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn nums[index];\n}\n/* \u66f4\u65b0\u5143\u7d20 */\npublic void set(int index, int num)\n{\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nnums[index] = num;\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\npublic void add(int num)\n{\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\nnums[numsSize] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\npublic void insert(int index, int num)\n{\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (numsSize == numsCapacity)\nextendCapacity();\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor (int j = numsSize - 1; j >= index; j--)\n{\nnums[j + 1] = nums[j];\n}\nnums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize++;\n}\n/* \u5220\u9664\u5143\u7d20 */\npublic int remove(int index)\n{\nif (index < 0 || index >= numsSize)\nthrow new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\nint num = nums[index];\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor (int j = index; j < numsSize - 1; j++)\n{\nnums[j] = nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nnumsSize--;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n/* \u5217\u8868\u6269\u5bb9 */\npublic void extendCapacity()\n{\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a numsCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nSystem.Array.Resize(ref nums, numsCapacity * extendRatio);\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nnumsCapacity = nums.Length;\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\npublic int[] toArray()\n{\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] nums = new int[numsSize];\nfor (int i = 0; i < numsSize; i++)\n{\nnums[i] = get(i);\n}\nreturn nums;\n}\n}\n</code></pre> my_list.swift<pre><code>/* \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0 */\nclass MyList {\nprivate var nums: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nprivate var _capacity = 10 // \u5217\u8868\u5bb9\u91cf\nprivate var _size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nprivate let extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n/* \u6784\u9020\u65b9\u6cd5 */\ninit() {\nnums = Array(repeating: 0, count: _capacity)\n}\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\nfunc size() -> Int {\n_size\n}\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc capacity() -> Int {\n_capacity\n}\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc get(index: Int) -> Int {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nreturn nums[index]\n}\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc set(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nnums[index] = num\n}\n/* \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc add(num: Int) {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\nnums[_size] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc insert(index: Int, num: Int) {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif _size == _capacity {\nextendCapacity()\n}\n// \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfor j in sequence(first: _size - 1, next: { $0 >= index + 1 ? $0 - 1 : nil }) {\nnums[j + 1] = nums[j]\n}\nnums[index] = num\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size += 1\n}\n/* \u5220\u9664\u5143\u7d20 */\n@discardableResult\nfunc remove(index: Int) -> Int {\nif index < 0 || index >= _size {\nfatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n}\nlet num = nums[index]\n// \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nfor j in index ..< (_size - 1) {\nnums[j] = nums[j + 1]\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n_size -= 1\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num\n}\n/* \u5217\u8868\u6269\u5bb9 */\nfunc extendCapacity() {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nnums = nums + Array(repeating: 0, count: _capacity * (extendRatio - 1))\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n_capacity = nums.count\n}\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\nfunc toArray() -> [Int] {\nvar nums = Array(repeating: 0, count: _size)\nfor i in 0 ..< _size {\nnums[i] = get(index: i)\n}\nreturn nums\n}\n}\n</code></pre> my_list.zig<pre><code>// \u5217\u8868\u7c7b\u7b80\u6613\u5b9e\u73b0\nfn MyList(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\nnumsCapacity: usize = 10, // \u5217\u8868\u5bb9\u91cf\nnumSize: usize = 0, // \u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\nextendRatio: usize = 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.nums = try self.mem_allocator.alloc(T, self.numsCapacity);\nstd.mem.set(T, self.nums, @as(T, 0));\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5373\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\npub fn size(self: *Self) usize {\nreturn self.numSize;\n}\n// \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.numsCapacity;\n}\n// \u8bbf\u95ee\u5143\u7d20\npub fn get(self: *Self, index: usize) T {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nreturn self.nums[index];\n} // \u66f4\u65b0\u5143\u7d20\npub fn set(self: *Self, index: usize, num: T) void {\n// \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nself.nums[index] = num;\n} // \u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\npub fn add(self: *Self, num: T) !void {\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (self.size() == self.capacity()) try self.extendCapacity();\nself.nums[self.size()] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 1;\n} // \u4e2d\u95f4\u63d2\u5165\u5143\u7d20\npub fn insert(self: *Self, index: usize, num: T) !void {\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n// \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\nif (self.size() == self.capacity()) try self.extendCapacity();\n// \u7d22\u5f15 i \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nvar j = self.size() - 1;\nwhile (j >= index) : (j -= 1) {\nself.nums[j + 1] = self.nums[j];\n}\nself.nums[index] = num;\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize += 1;\n}\n// \u5220\u9664\u5143\u7d20\npub fn remove(self: *Self, index: usize) T {\nif (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\nvar num = self.nums[index];\n// \u7d22\u5f15 i \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\nvar j = index;\nwhile (j < self.size() - 1) : (j += 1) {\nself.nums[j] = self.nums[j + 1];\n}\n// \u66f4\u65b0\u5143\u7d20\u6570\u91cf\nself.numSize -= 1;\n// \u8fd4\u56de\u88ab\u5220\u9664\u5143\u7d20\nreturn num;\n}\n// \u5217\u8868\u6269\u5bb9\npub fn extendCapacity(self: *Self) !void {\n// \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u62f7\u8d1d\u5230\u65b0\u6570\u7ec4\nvar newCapacity = self.capacity() * self.extendRatio;\nvar extend = try self.mem_allocator.alloc(T, newCapacity);\nstd.mem.set(T, extend, @as(T, 0));\n// \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\nstd.mem.copy(T, extend, self.nums);\nself.nums = extend;\n// \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\nself.numsCapacity = newCapacity;\n}\n// \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar nums = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, nums, @as(T, 0));\nfor (nums) |*num, i| {\nnum.* = self.get(i);\n}\nreturn nums;\n}\n};\n}\n</code></pre>"},{"location":"chapter_array_and_linkedlist/summary/","title":"4.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u6570\u7ec4\u548c\u94fe\u8868\u662f\u4e24\u79cd\u57fa\u672c\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u4e24\u79cd\u5b58\u50a8\u65b9\u5f0f\uff0c\u5373\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\u3002\u4e24\u8005\u7684\u4f18\u70b9\u4e0e\u7f3a\u70b9\u5448\u73b0\u51fa\u6b64\u6d88\u5f7c\u957f\u7684\u5173\u7cfb\u3002</li> <li>\u6570\u7ec4\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3001\u5185\u5b58\u7a7a\u95f4\u5360\u7528\u5c0f\uff1b\u4f46\u63d2\u5165\u4e0e\u5220\u9664\u5143\u7d20\u6548\u7387\u4f4e\uff0c\u4e14\u521d\u59cb\u5316\u540e\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002</li> <li>\u94fe\u8868\u53ef\u901a\u8fc7\u66f4\u6539\u6307\u9488\u5b9e\u73b0\u9ad8\u6548\u7684\u7ed3\u70b9\u63d2\u5165\u4e0e\u5220\u9664\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5730\u4fee\u6539\u957f\u5ea6\uff1b\u4f46\u7ed3\u70b9\u8bbf\u95ee\u6548\u7387\u4f4e\u3001\u5360\u7528\u5185\u5b58\u591a\u3002\u5e38\u89c1\u7684\u94fe\u8868\u7c7b\u578b\u6709\u5355\u5411\u94fe\u8868\u3001\u5faa\u73af\u94fe\u8868\u3001\u53cc\u5411\u94fe\u8868\u3002</li> <li>\u5217\u8868\u53c8\u79f0\u52a8\u6001\u6570\u7ec4\uff0c\u662f\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4fdd\u5b58\u4e86\u6570\u7ec4\u7684\u4f18\u52bf\uff0c\u4e14\u53ef\u4ee5\u7075\u6d3b\u6539\u53d8\u957f\u5ea6\u3002\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\u3002</li> <li>\u4e0b\u8868\u603b\u7ed3\u5bf9\u6bd4\u4e86\u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u5404\u9879\u7279\u6027\u3002</li> </ul> \u6570\u7ec4 \u94fe\u8868 \u5b58\u50a8\u65b9\u5f0f \u8fde\u7eed\u5185\u5b58\u7a7a\u95f4 \u79bb\u6563\u5185\u5b58\u7a7a\u95f4 \u6570\u636e\u7ed3\u6784\u957f\u5ea6 \u957f\u5ea6\u4e0d\u53ef\u53d8 \u957f\u5ea6\u53ef\u53d8 \u5185\u5b58\u4f7f\u7528\u7387 \u5360\u7528\u5185\u5b58\u5c11\u3001\u7f13\u5b58\u5c40\u90e8\u6027\u597d \u5360\u7528\u5185\u5b58\u591a \u4f18\u52bf\u64cd\u4f5c \u968f\u673a\u8bbf\u95ee \u63d2\u5165\u3001\u5220\u9664 <p>\u7f13\u5b58\u5c40\u90e8\u6027\u7684\u7b80\u5355\u89e3\u91ca</p> <p>\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u6570\u636e\u8bfb\u5199\u901f\u5ea6\u6392\u5e8f\u662f\u201c\u786c\u76d8 < \u5185\u5b58 < CPU \u7f13\u5b58\u201d\u3002\u5f53\u6211\u4eec\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u65f6\uff0c\u8ba1\u7b97\u673a\u4e0d\u4ec5\u4f1a\u52a0\u8f7d\u5b83\uff0c\u8fd8\u4f1a\u7f13\u5b58\u5176\u5468\u56f4\u7684\u5176\u5b83\u6570\u636e\uff0c\u4ece\u800c\u501f\u52a9\u9ad8\u901f\u7f13\u5b58\u6765\u63d0\u5347\u540e\u7eed\u64cd\u4f5c\u7684\u6267\u884c\u901f\u5ea6\u3002\u94fe\u8868\u5219\u4e0d\u7136\uff0c\u8ba1\u7b97\u673a\u53ea\u80fd\u6328\u4e2a\u5730\u7f13\u5b58\u5404\u4e2a\u7ed3\u70b9\uff0c\u8fd9\u6837\u7684\u591a\u6b21\u201c\u642c\u8fd0\u201d\u964d\u4f4e\u4e86\u6574\u4f53\u6548\u7387\u3002</p> <ul> <li>\u4e0b\u8868\u5bf9\u6bd4\u4e86\u6570\u7ec4\u4e0e\u94fe\u8868\u7684\u5404\u79cd\u64cd\u4f5c\u6548\u7387\u3002</li> </ul> \u64cd\u4f5c \u6570\u7ec4 \u94fe\u8868 \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_computational_complexity/performance_evaluation/","title":"2.1. \u00a0 \u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":""},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1. \u00a0 \u7b97\u6cd5\u8bc4\u4ef7\u7ef4\u5ea6","text":"<p>\u5728\u5f00\u59cb\u5b66\u4e60\u7b97\u6cd5\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u8981\u60f3\u6e05\u695a\u7b97\u6cd5\u7684\u8bbe\u8ba1\u76ee\u6807\u662f\u4ec0\u4e48\uff0c\u6216\u8005\u8bf4\uff0c\u5982\u4f55\u6765\u8bc4\u5224\u7b97\u6cd5\u7684\u597d\u4e0e\u574f\u3002\u6574\u4f53\u4e0a\u770b\uff0c\u6211\u4eec\u8bbe\u8ba1\u7b97\u6cd5\u65f6\u8ffd\u6c42\u4e24\u4e2a\u5c42\u9762\u7684\u76ee\u6807\u3002</p> <ol> <li>\u627e\u5230\u95ee\u9898\u89e3\u6cd5\u3002\u7b97\u6cd5\u9700\u8981\u80fd\u591f\u5728\u89c4\u5b9a\u7684\u8f93\u5165\u8303\u56f4\u4e0b\uff0c\u53ef\u9760\u5730\u6c42\u5f97\u95ee\u9898\u7684\u6b63\u786e\u89e3\u3002</li> <li>\u5bfb\u6c42\u6700\u4f18\u89e3\u6cd5\u3002\u540c\u4e00\u4e2a\u95ee\u9898\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u89e3\u6cd5\uff0c\u800c\u6211\u4eec\u5e0c\u671b\u7b97\u6cd5\u6548\u7387\u5c3d\u53ef\u80fd\u7684\u9ad8\u3002</li> </ol> <p>\u6362\u8a00\u4e4b\uff0c\u5728\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\u7684\u524d\u63d0\u4e0b\uff0c\u7b97\u6cd5\u6548\u7387\u5219\u662f\u4e3b\u8981\u8bc4\u4ef7\u7ef4\u5ea6\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u65f6\u95f4\u6548\u7387\uff0c\u5373\u7b97\u6cd5\u7684\u8fd0\u884c\u901f\u5ea6\u7684\u5feb\u6162\u3002</li> <li>\u7a7a\u95f4\u6548\u7387\uff0c\u5373\u7b97\u6cd5\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5927\u5c0f\u3002</li> </ul> <p>\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u8ffd\u6c42\u201c\u8fd0\u884c\u901f\u5ea6\u5feb\u3001\u5360\u7528\u5185\u5b58\u5c11\u201d\uff0c\u800c\u5982\u4f55\u53bb\u8bc4\u4ef7\u7b97\u6cd5\u6548\u7387\u5219\u662f\u975e\u5e38\u91cd\u8981\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u53ea\u6709\u77e5\u9053\u5982\u4f55\u8bc4\u4ef7\u7b97\u6cd5\uff0c\u624d\u80fd\u53bb\u505a\u7b97\u6cd5\u4e4b\u95f4\u7684\u5bf9\u6bd4\u5206\u6790\uff0c\u4ee5\u53ca\u4f18\u5316\u7b97\u6cd5\u8bbe\u8ba1\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2. \u00a0 \u6548\u7387\u8bc4\u4f30\u65b9\u6cd5","text":""},{"location":"chapter_computational_complexity/performance_evaluation/#_1","title":"\u5b9e\u9645\u6d4b\u8bd5","text":"<p>\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u7b97\u6cd5 A \u548c \u7b97\u6cd5 B \uff0c\u90fd\u80fd\u591f\u89e3\u51b3\u540c\u4e00\u95ee\u9898\uff0c\u73b0\u5728\u9700\u8981\u5bf9\u6bd4\u4e24\u4e2a\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u3002\u6211\u4eec\u80fd\u591f\u60f3\u5230\u7684\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u5c31\u662f\u627e\u4e00\u53f0\u8ba1\u7b97\u673a\uff0c\u628a\u4e24\u4e2a\u7b97\u6cd5\u90fd\u5b8c\u6574\u8dd1\u4e00\u904d\uff0c\u5e76\u76d1\u63a7\u8bb0\u5f55\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\u662f\u4e5f\u5b58\u5728\u5f88\u5927\u7684\u786c\u4f24\u3002</p> <p>\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\u56e0\u7d20\u3002\u786c\u4ef6\u914d\u7f6e\u4f1a\u5f71\u54cd\u5230\u7b97\u6cd5\u7684\u6027\u80fd\u8868\u73b0\u3002\u4f8b\u5982\uff0c\u5728\u67d0\u53f0\u8ba1\u7b97\u673a\u4e2d\uff0c\u7b97\u6cd5 A \u6bd4\u7b97\u6cd5 B \u8fd0\u884c\u65f6\u95f4\u66f4\u77ed\uff1b\u4f46\u6362\u5230\u53e6\u4e00\u53f0\u914d\u7f6e\u4e0d\u540c\u7684\u8ba1\u7b97\u673a\u4e2d\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u76f8\u53cd\u7684\u6d4b\u8bd5\u7ed3\u679c\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u9700\u8981\u5728\u5404\u79cd\u673a\u5668\u4e0a\u5c55\u5f00\u6d4b\u8bd5\uff0c\u800c\u8fd9\u662f\u4e0d\u73b0\u5b9e\u7684\u3002</p> <p>\u5c55\u5f00\u5b8c\u6574\u6d4b\u8bd5\u975e\u5e38\u8017\u8d39\u8d44\u6e90\u3002\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u7684\u5927\u5c0f\u53d8\u5316\uff0c\u7b97\u6cd5\u4f1a\u5448\u73b0\u51fa\u4e0d\u540c\u7684\u6548\u7387\u8868\u73b0\u3002\u6bd4\u5982\uff0c\u6709\u53ef\u80fd\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 A \u8fd0\u884c\u65f6\u95f4\u77ed\u4e8e\u7b97\u6cd5 B \uff0c\u800c\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6d4b\u8bd5\u7ed3\u679c\u622a\u7136\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u82e5\u60f3\u8981\u8fbe\u5230\u5177\u6709\u8bf4\u670d\u529b\u7684\u5bf9\u6bd4\u7ed3\u679c\uff0c\u90a3\u4e48\u9700\u8981\u8f93\u5165\u5404\u79cd\u4f53\u91cf\u6570\u636e\uff0c\u8fd9\u6837\u7684\u6d4b\u8bd5\u9700\u8981\u5360\u7528\u5927\u91cf\u8ba1\u7b97\u8d44\u6e90\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#_2","title":"\u7406\u8bba\u4f30\u7b97","text":"<p>\u65e2\u7136\u5b9e\u9645\u6d4b\u8bd5\u5177\u6709\u5f88\u5927\u7684\u5c40\u9650\u6027\uff0c\u90a3\u4e48\u6211\u4eec\u662f\u5426\u53ef\u4ee5\u4ec5\u901a\u8fc7\u4e00\u4e9b\u8ba1\u7b97\uff0c\u5c31\u83b7\u77e5\u7b97\u6cd5\u7684\u6548\u7387\u6c34\u5e73\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u6211\u4eec\u5c06\u6b64\u4f30\u7b97\u65b9\u6cd5\u79f0\u4e3a\u300c\u590d\u6742\u5ea6\u5206\u6790 Complexity Analysis\u300d\u6216\u300c\u6e10\u8fd1\u590d\u6742\u5ea6\u5206\u6790 Asymptotic Complexity Analysis\u300d\u3002</p> <p>\u590d\u6742\u5ea6\u5206\u6790\u8bc4\u4f30\u7684\u662f\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u53e5\u8bdd\u6709\u4e9b\u62d7\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u4e3a\u4e09\u4e2a\u91cd\u70b9\u6765\u7406\u89e3\uff1a</p> <ul> <li>\u201c\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u201d\u53ef\u5206\u4e3a\u201c\u8fd0\u884c\u65f6\u95f4\u201d\u548c\u201c\u5360\u7528\u7a7a\u95f4\u201d\uff0c\u8fdb\u800c\u53ef\u5c06\u590d\u6742\u5ea6\u5206\u4e3a\u300c\u65f6\u95f4\u590d\u6742\u5ea6 Time Complexity\u300d\u548c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\u3002</li> <li>\u201c\u968f\u7740\u8f93\u5165\u6570\u636e\u91cf\u589e\u591a\u65f6\u201d\u4ee3\u8868\u590d\u6742\u5ea6\u4e0e\u8f93\u5165\u6570\u636e\u91cf\u6709\u5173\uff0c\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u4e0e\u8f93\u5165\u6570\u636e\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\uff1b</li> <li>\u201c\u589e\u957f\u8d8b\u52bf\u201d\u8868\u793a\u590d\u6742\u5ea6\u5206\u6790\u4e0d\u5173\u5fc3\u7b97\u6cd5\u5177\u4f53\u4f7f\u7528\u4e86\u591a\u5c11\u65f6\u95f4\u6216\u5360\u7528\u4e86\u591a\u5c11\u7a7a\u95f4\uff0c\u800c\u662f\u7ed9\u51fa\u4e00\u79cd\u201c\u8d8b\u52bf\u6027\u5206\u6790\u201d\uff1b</li> </ul> <p>\u590d\u6742\u5ea6\u5206\u6790\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5f0a\u7aef\u3002\u4e00\u662f\u72ec\u7acb\u4e8e\u6d4b\u8bd5\u73af\u5883\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\u3002\u4e8c\u662f\u53ef\u4ee5\u4f53\u73b0\u4e0d\u540c\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\uff0c\u5c24\u5176\u662f\u53ef\u4ee5\u53cd\u6620\u5927\u6570\u636e\u91cf\u4e0b\u7684\u7b97\u6cd5\u6027\u80fd\u3002</p> <p>\u5982\u679c\u611f\u89c9\u5bf9\u590d\u6742\u5ea6\u5206\u6790\u7684\u6982\u5ff5\u4e00\u77e5\u534a\u89e3\uff0c\u65e0\u9700\u62c5\u5fc3\uff0c\u540e\u7eed\u7ae0\u8282\u4f1a\u5c55\u5f00\u4ecb\u7ecd\u3002</p>"},{"location":"chapter_computational_complexity/performance_evaluation/#213","title":"2.1.3. \u00a0 \u590d\u6742\u5ea6\u5206\u6790\u91cd\u8981\u6027","text":"<p>\u590d\u6742\u5ea6\u5206\u6790\u7ed9\u51fa\u4e00\u628a\u8bc4\u4ef7\u7b97\u6cd5\u6548\u7387\u7684\u201c\u6807\u5c3a\u201d\uff0c\u544a\u8bc9\u6211\u4eec\u6267\u884c\u67d0\u4e2a\u7b97\u6cd5\u9700\u8981\u591a\u5c11\u65f6\u95f4\u548c\u7a7a\u95f4\u8d44\u6e90\uff0c\u4e5f\u8ba9\u6211\u4eec\u53ef\u4ee5\u5f00\u5c55\u4e0d\u540c\u7b97\u6cd5\u4e4b\u95f4\u7684\u6548\u7387\u5bf9\u6bd4\u3002</p> <p>\u8ba1\u7b97\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\u51fa\u53d1\uff0c\u5176\u5e76\u4e0d\u9002\u5408\u4f5c\u4e3a\u7b2c\u4e00\u7ae0\u5185\u5bb9\u3002\u4f46\u662f\uff0c\u5f53\u6211\u4eec\u8ba8\u8bba\u67d0\u4e2a\u6570\u636e\u7ed3\u6784\u6216\u8005\u7b97\u6cd5\u7684\u7279\u70b9\u65f6\uff0c\u96be\u4ee5\u907f\u514d\u9700\u8981\u5206\u6790\u5b83\u7684\u8fd0\u884c\u901f\u5ea6\u548c\u7a7a\u95f4\u4f7f\u7528\u60c5\u51b5\u3002\u56e0\u6b64\uff0c\u5728\u5c55\u5f00\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e4b\u524d\uff0c\u5efa\u8bae\u8bfb\u8005\u5148\u5bf9\u8ba1\u7b97\u590d\u6742\u5ea6\u5efa\u7acb\u8d77\u521d\u6b65\u7684\u4e86\u89e3\uff0c\u5e76\u4e14\u80fd\u591f\u5b8c\u6210\u7b80\u5355\u6848\u4f8b\u7684\u590d\u6742\u5ea6\u5206\u6790\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.3. \u00a0 \u7a7a\u95f4\u590d\u6742\u5ea6","text":"<p>\u300c\u7a7a\u95f4\u590d\u6742\u5ea6 Space Complexity\u300d\u7edf\u8ba1 \u7b97\u6cd5\u4f7f\u7528\u5185\u5b58\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002\u8fd9\u4e2a\u6982\u5ff5\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u5f88\u7c7b\u4f3c\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#231","title":"2.3.1. \u00a0 \u7b97\u6cd5\u76f8\u5173\u7a7a\u95f4","text":"<p>\u7b97\u6cd5\u8fd0\u884c\u4e2d\uff0c\u4f7f\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a</p> <ul> <li>\u300c\u8f93\u5165\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u5165\u6570\u636e\uff1b</li> <li>\u300c\u6682\u5b58\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u53d8\u91cf\u3001\u5bf9\u8c61\u3001\u51fd\u6570\u4e0a\u4e0b\u6587\u7b49\u6570\u636e\uff1b</li> <li>\u300c\u8f93\u51fa\u7a7a\u95f4\u300d\u7528\u4e8e\u5b58\u50a8\u7b97\u6cd5\u7684\u8f93\u51fa\u6570\u636e\uff1b</li> </ul> <p>Tip</p> <p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u7edf\u8ba1\u8303\u56f4\u662f\u300c\u6682\u5b58\u7a7a\u95f4\u300d+\u300c\u8f93\u51fa\u7a7a\u95f4\u300d\u3002</p> <p>\u6682\u5b58\u7a7a\u95f4\u53ef\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a</p> <ul> <li>\u300c\u6682\u5b58\u6570\u636e\u300d\u7528\u4e8e\u4fdd\u5b58\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u5404\u79cd \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61 \u7b49\u3002</li> <li>\u300c\u6808\u5e27\u7a7a\u95f4\u300d\u7528\u4e8e\u4fdd\u5b58\u8c03\u7528\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u6570\u636e\u3002\u7cfb\u7edf\u6bcf\u6b21\u8c03\u7528\u51fd\u6570\u90fd\u4f1a\u5728\u6808\u7684\u9876\u90e8\u521b\u5efa\u4e00\u4e2a\u6808\u5e27\uff0c\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u6808\u5e27\u7a7a\u95f4\u4f1a\u88ab\u91ca\u653e\u3002</li> <li>\u300c\u6307\u4ee4\u7a7a\u95f4\u300d\u7528\u4e8e\u4fdd\u5b58\u7f16\u8bd1\u540e\u7684\u7a0b\u5e8f\u6307\u4ee4\uff0c\u5728\u5b9e\u9645\u7edf\u8ba1\u4e2d\u4e00\u822c\u5ffd\u7565\u4e0d\u8ba1\u3002</li> </ul> <p></p> <p> Fig. \u7b97\u6cd5\u4f7f\u7528\u7684\u76f8\u5173\u7a7a\u95f4 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>/* \u7c7b */\nclass Node {\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nfinal int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7ed3\u6784\u4f53 */\nstruct Node {\nint val;\nNode *next;\nNode(int x) : val(x), next(nullptr) {}\n};\n/* \u51fd\u6570 */\nint func() {\n// do something...\nreturn 0;\n}\nint algorithm(int n) { // \u8f93\u5165\u6570\u636e\nconst int a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode* node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = func(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>\"\"\" \u7c7b \"\"\"\nclass Node:\ndef __init__(self, x):\nself.val = x # \u7ed3\u70b9\u503c\nself.next = None # \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u6307\u9488\uff08\u5f15\u7528\uff09\n\"\"\" \u51fd\u6570 \"\"\"\ndef function():\n# do something...\nreturn 0\ndef algorithm(n): # \u8f93\u5165\u6570\u636e\nb = 0 # \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnode = Node(0) # \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc = function() # \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c # \u8f93\u51fa\u6570\u636e\n</code></pre> <pre><code>/* \u7ed3\u6784\u4f53 */\ntype node struct {\nval int\nnext *node\n}\n/* \u521b\u5efa node \u7ed3\u6784\u4f53 */\nfunc newNode(val int) *node {\nreturn &node{val: val}\n}\n/* \u51fd\u6570 */\nfunc function() int {\n// do something...\nreturn 0\n}\nfunc algorithm(n int) int { // \u8f93\u5165\u6570\u636e\nconst a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nb := 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nnewNode(0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nc := function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nval;\nnext;\nconstructor(val) {\nthis.val = val === undefined ? 0 : val; // \u7ed3\u70b9\u503c\nthis.next = null; // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc() {\n// do something\nreturn 0;\n}\nfunction algorithm(n) { // \u8f93\u5165\u6570\u636e\nconst a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nconst 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; // \u7ed3\u70b9\u503c\nthis.next = null; // \u6307\u5411\u4e0b\u4e00\u7ed3\u70b9\u7684\u5f15\u7528\n}\n}\n/* \u51fd\u6570 */\nfunction constFunc(): number {\n// do something\nreturn 0;\n}\nfunction algorithm(n: number): number { // \u8f93\u5165\u6570\u636e\nconst a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nconst 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>\n</code></pre> <pre><code>/* \u7c7b */\nclass Node\n{\nint val;\nNode next;\nNode(int x) { val = x; }\n}\n/* \u51fd\u6570 */\nint function()\n{\n// do something...\nreturn 0;\n}\nint algorithm(int n) // \u8f93\u5165\u6570\u636e\n{\nint a = 0; // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nint b = 0; // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nNode node = new Node(0); // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nint c = function(); // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c; // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>/* \u7c7b */\nclass Node {\nvar val: Int\nvar next: Node?\ninit(x: Int) {\nval = x\n}\n}\n/* \u51fd\u6570 */\nfunc function() -> Int {\n// do something...\nreturn 0\n}\nfunc algorithm(n: Int) -> Int { // \u8f93\u5165\u6570\u636e\nlet a = 0 // \u6682\u5b58\u6570\u636e\uff08\u5e38\u91cf\uff09\nvar b = 0 // \u6682\u5b58\u6570\u636e\uff08\u53d8\u91cf\uff09\nlet node = Node(x: 0) // \u6682\u5b58\u6570\u636e\uff08\u5bf9\u8c61\uff09\nlet c = function() // \u6808\u5e27\u7a7a\u95f4\uff08\u8c03\u7528\u51fd\u6570\uff09\nreturn a + b + c // \u8f93\u51fa\u6570\u636e\n}\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#232","title":"2.3.2. \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u548c\u65f6\u95f4\u590d\u6742\u5ea6\u603b\u4f53\u7c7b\u4f3c\uff0c\u53ea\u662f\u4ece\u7edf\u8ba1\u201c\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u201d\u53d8\u4e3a\u7edf\u8ba1\u201c\u4f7f\u7528\u7a7a\u95f4\u5927\u5c0f\u201d\u3002\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u540c\u7684\u662f\uff0c\u6211\u4eec\u4e00\u822c\u53ea\u5173\u6ce8\u300c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u300d\u3002\u8fd9\u662f\u56e0\u4e3a\u5185\u5b58\u7a7a\u95f4\u662f\u4e00\u4e2a\u786c\u6027\u8981\u6c42\uff0c\u6211\u4eec\u5fc5\u987b\u4fdd\u8bc1\u5728\u6240\u6709\u8f93\u5165\u6570\u636e\u4e0b\u90fd\u6709\u8db3\u591f\u7684\u5185\u5b58\u7a7a\u95f4\u9884\u7559\u3002</p> <p>\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e2d\u7684\u201c\u6700\u5dee\u201d\u6709\u4e24\u5c42\u542b\u4e49\uff0c\u5206\u522b\u4e3a\u8f93\u5165\u6570\u636e\u7684\u6700\u5dee\u5206\u5e03\u3001\u7b97\u6cd5\u8fd0\u884c\u4e2d\u7684\u6700\u5dee\u65f6\u95f4\u70b9\u3002</p> <ul> <li>\u4ee5\u6700\u5dee\u8f93\u5165\u6570\u636e\u4e3a\u51c6\u3002\u5f53 \\(n < 10\\) \u65f6\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff1b\u4f46\u662f\u5f53 \\(n > 10\\) \u65f6\uff0c\u521d\u59cb\u5316\u7684\u6570\u7ec4 <code>nums</code> \u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b</li> <li>\u4ee5\u7b97\u6cd5\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u5cf0\u503c\u5185\u5b58\u4e3a\u51c6\u3002\u7a0b\u5e8f\u5728\u6267\u884c\u6700\u540e\u4e00\u884c\u4e4b\u524d\uff0c\u4f7f\u7528 \\(O(1)\\) \u7a7a\u95f4\uff1b\u5f53\u521d\u59cb\u5316\u6570\u7ec4 <code>nums</code> \u65f6\uff0c\u7a0b\u5e8f\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\uff1b\u56e0\u6b64\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \uff1b</li> </ul> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <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)\nvector<int> b(10000); // O(1)\nif (n > 10)\nvector<int> nums(n); // O(n)\n}\n</code></pre> <pre><code>def algorithm(n):\na = 0 # O(1)\nb = [0] * 10000 # O(1)\nif n > 10:\nnums = [0] * n # O(n)\n</code></pre> <pre><code>func algorithm(n int) {\na := 0 // O(1)\nb := make([]int, 10000) // O(1)\nvar nums []int\nif n > 10 {\nnums := make([]int, n) // O(n)\n}\nfmt.Println(a, b, nums)\n}\n</code></pre> <pre><code>function algorithm(n) {\nconst a = 0; // O(1)\nconst b = new Array(10000); // O(1)\nif (n > 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>\n</code></pre> <pre><code>void algorithm(int n)\n{\nint a = 0; // O(1)\nint[] b = new int[10000]; // O(1)\nif (n > 10)\n{\nint[] nums = new int[n]; // O(n)\n}\n}\n</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>\n</code></pre> <p>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7edf\u8ba1\u6808\u5e27\u7a7a\u95f4\u3002\u4f8b\u5982\u51fd\u6570 <code>loop()</code>\uff0c\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\u800c\u9012\u5f52\u51fd\u6570 <code>recur()</code> \u5728\u8fd0\u884c\u4e2d\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 <code>recur()</code> \uff0c\u4ece\u800c\u4f7f\u7528 \\(O(n)\\) \u7684\u6808\u5e27\u7a7a\u95f4\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>int function() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>int func() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n) {\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nvoid recur(int n) {\nif (n == 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>def function():\n# do something\nreturn 0\n\"\"\" \u5faa\u73af O(1) \"\"\"\ndef loop(n):\nfor _ in range(n):\nfunction()\n\"\"\" \u9012\u5f52 O(n) \"\"\"\ndef recur(n):\nif n == 1: return\nreturn recur(n - 1)\n</code></pre> <pre><code>func function() int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n int) {\nfor i := 0; i < n; i++ {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n int) {\nif n == 1 {\nreturn\n}\nrecur(n - 1)\n}\n</code></pre> <pre><code>function constFunc() {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n) {\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n/* \u9012\u5f52 O(n) */\nfunction recur(n) {\nif (n === 1) return;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>function constFunc(): number {\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nfunction loop(n: number): void {\nfor (let i = 0; i < 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>\n</code></pre> <pre><code>int function()\n{\n// do something\nreturn 0;\n}\n/* \u5faa\u73af O(1) */\nvoid loop(int n)\n{\nfor (int i = 0; i < n; i++)\n{\nfunction();\n}\n}\n/* \u9012\u5f52 O(n) */\nint recur(int n)\n{\nif (n == 1) return 1;\nreturn recur(n - 1);\n}\n</code></pre> <pre><code>@discardableResult\nfunc function() -> Int {\n// do something\nreturn 0\n}\n/* \u5faa\u73af O(1) */\nfunc loop(n: Int) {\nfor _ in 0 ..< n {\nfunction()\n}\n}\n/* \u9012\u5f52 O(n) */\nfunc recur(n: Int) {\nif n == 1 {\nreturn\n}\nrecur(n: n - 1)\n}\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#233","title":"2.3.3. \u00a0 \u5e38\u89c1\u7c7b\u578b","text":"<p>\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u5e38\u89c1\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7c7b\u578b\u6709\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09</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> Fig. \u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b </p> <p>Tip</p> <p>\u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u524d\u7f6e\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\u3001\u9012\u5f52\u7b97\u6cd5\u7b49\u3002\u5982\u679c\u9047\u5230\u770b\u4e0d\u61c2\u7684\u5730\u65b9\u65e0\u9700\u62c5\u5fc3\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\uff0c\u73b0\u9636\u6bb5\u5148\u805a\u7126\u5728\u7406\u89e3\u7a7a\u95f4\u590d\u6742\u5ea6\u542b\u4e49\u548c\u63a8\u7b97\u65b9\u6cd5\u4e0a\u3002</p>"},{"location":"chapter_computational_complexity/space_complexity/#o1","title":"\u5e38\u6570\u9636 \\(O(1)\\)","text":"<p>\u5e38\u6570\u9636\u5e38\u89c1\u4e8e\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u7684\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u3002</p> <p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u5faa\u73af\u4e2d\u521d\u59cb\u5316\u53d8\u91cf\u6216\u8c03\u7528\u51fd\u6570\u800c\u5360\u7528\u7684\u5185\u5b58\uff0c\u5728\u8fdb\u5165\u4e0b\u4e00\u5faa\u73af\u540e\u5c31\u4f1a\u88ab\u91ca\u653e\uff0c\u5373\u4e0d\u4f1a\u7d2f\u79ef\u5360\u7528\u7a7a\u95f4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(1)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig space_complexity.java<pre><code>/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nfinal int a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunction();\n}\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst int a = 0;\nint b = 0;\nvector<int> nums(10000);\nListNode node(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++) {\nfunc();\n}\n}\n</code></pre> space_complexity.py<pre><code>\"\"\" \u5e38\u6570\u9636 \"\"\"\ndef constant(n):\n# \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\na = 0\nnums = [0] * 10000\nnode = ListNode(0)\n# \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nc = 0\n# \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in range(n):\nfunction()\n</code></pre> space_complexity.go<pre><code>/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0\nb := 0\nnums := make([]int, 10000)\nListNode := newNode(0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nvar c int\nfor i := 0; i < n; i++ {\nc = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor i := 0; i < n; i++ {\nfunction()\n}\nfmt.Println(a, b, nums, c, ListNode)\n}\n</code></pre> space_complexity.js<pre><code>/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n</code></pre> space_complexity.ts<pre><code>/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a = 0;\nconst b = 0;\nconst nums = new Array(10000);\nconst node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconst c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (let i = 0; i < n; i++) {\nconstFunc();\n}\n}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{spaceConstant}\n</code></pre> space_complexity.cs<pre><code>/* \u5e38\u6570\u9636 */\nvoid constant(int n)\n{\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nint a = 0;\nint b = 0;\nint[] nums = new int[10000];\nListNode node = new ListNode(0);\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++)\n{\nint c = 0;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor (int i = 0; i < n; i++)\n{\nfunction();\n}\n}\n</code></pre> space_complexity.swift<pre><code>/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nlet a = 0\nvar b = 0\nlet nums = Array(repeating: 0, count: 10000)\nlet node = ListNode(x: 0)\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..< n {\nlet c = 0\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\nfor _ in 0 ..< n {\nfunction()\n}\n}\n</code></pre> space_complexity.zig<pre><code>// \u5e38\u6570\u9636\nfn constant(n: i32) void {\n// \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\nconst a: i32 = 0;\nvar b: i32 = 0;\nvar nums = [_]i32{0}**10000;\nvar node = inc.ListNode(i32){.val = 0};\nvar i: i32 = 0;\n// \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\nwhile (i < n) : (i += 1) {\nvar c: i32 = 0;\n_ = c;\n}\n// \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\ni = 0;\nwhile (i < n) : (i += 1) {\n_ = function();\n}\n_ = a;\n_ = b;\n_ = nums;\n_ = node;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/space_complexity/#on","title":"\u7ebf\u6027\u9636 \\(O(n)\\)","text":"<p>\u7ebf\u6027\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u6b63\u6bd4\u7684\u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u7b49\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig space_complexity.java<pre><code>/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nnodes.add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nMap<Integer, String> map = new HashMap<>();\nfor (int i = 0; i < n; i++) {\nmap.put(i, String.valueOf(i));\n}\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nvector<int> nums(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvector<ListNode> nodes;\nfor (int i = 0; i < n; i++) {\nnodes.push_back(ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nunordered_map<int, string> map;\nfor (int i = 0; i < n; i++) {\nmap[i] = to_string(i);\n}\n}\n</code></pre> space_complexity.py<pre><code>\"\"\" \u7ebf\u6027\u9636 \"\"\"\ndef linear(n):\n# \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nnums = [0] * n\n# \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nmapp = {}\nfor i in range(n):\nmapp[i] = str(i)\n</code></pre> space_complexity.go<pre><code>/* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n_ = make([]int, n)\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar nodes []*node\nfor i := 0; i < n; i++ {\nnodes = append(nodes, newNode(i))\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nm := make(map[int]string, n)\nfor i := 0; i < n; i++ {\nm[i] = strconv.Itoa(i)\n}\n}\n</code></pre> space_complexity.js<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes = [];\nfor (let i = 0; i < n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i < n; i++) {\nmap.set(i, i.toString());\n}\n}\n</code></pre> space_complexity.ts<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nconst nums = new Array(n);\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst nodes: ListNode[] = [];\nfor (let i = 0; i < n; i++) {\nnodes.push(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nconst map = new Map();\nfor (let i = 0; i < n; i++) {\nmap.set(i, i.toString());\n}\n}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{spaceLinear}\n</code></pre> space_complexity.cs<pre><code>/* \u7ebf\u6027\u9636 */\nvoid linear(int n)\n{\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nint[] nums = new int[n];\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nList<ListNode> nodes = new();\nfor (int i = 0; i < n; i++)\n{\nnodes.Add(new ListNode(i));\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nDictionary<int, String> map = new();\nfor (int i = 0; i < n; i++)\n{\nmap.Add(i, i.ToString());\n}\n}\n</code></pre> space_complexity.swift<pre><code>/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nlet nums = Array(repeating: 0, count: n)\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet nodes = (0 ..< n).map { ListNode(x: $0) }\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nlet map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n</code></pre> space_complexity.zig<pre><code>// \u7ebf\u6027\u9636\nfn linear(comptime n: i32) !void {\n// \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\nvar nums = [_]i32{0}**n;\n// \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar nodes = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\ntry nodes.append(i);\n}\n// \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\nvar map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\ndefer map.deinit();\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\nconst string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\ndefer std.heap.page_allocator.free(string);\ntry map.put(i, string);\n}\n_ = nums;\n}\n</code></pre> <p>\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4f1a\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 <code>algorithm()</code> \u51fd\u6570\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig space_complexity.java<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\nSystem.out.println(\"\u9012\u5f52 n = \" + n);\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\ncout << \"\u9012\u5f52 n = \" << n << endl;\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.py<pre><code>\"\"\" \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 \"\"\"\ndef linear_recur(n):\nprint(\"\u9012\u5f52 n =\", n)\nif n == 1: return\nlinear_recur(n - 1)\n</code></pre> space_complexity.go<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\nfmt.Println(\"\u9012\u5f52 n =\", n)\nif n == 1 {\nreturn\n}\nspaceLinearRecur(n - 1)\n}\n</code></pre> space_complexity.js<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.ts<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\nconsole.log(`\u9012\u5f52 n = ${n}`);\nif (n === 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{spaceLinearRecur}\n</code></pre> space_complexity.cs<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n)\n{\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n);\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> space_complexity.swift<pre><code>/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\nprint(\"\u9012\u5f52 n = \\(n)\")\nif n == 1 {\nreturn\n}\nlinearRecur(n: n - 1)\n}\n</code></pre> space_complexity.zig<pre><code>// \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\nstd.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\nif (n == 1) return;\nlinearRecur(n - 1);\n}\n</code></pre> <p></p> <p> Fig. \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u7ebf\u6027\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#on2","title":"\u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"<p>\u5e73\u65b9\u9636\u5e38\u89c1\u4e8e\u5143\u7d20\u6570\u91cf\u4e0e \\(n\\) \u6210\u5e73\u65b9\u5173\u7cfb\u7684\u77e9\u9635\u3001\u56fe\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig space_complexity.java<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[][] numMatrix = new int[n][n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<Integer>> numList = new ArrayList<>();\nfor (int i = 0; i < n; i++) {\nList<Integer> tmp = new ArrayList<>();\nfor (int j = 0; j < n; j++) {\ntmp.add(0);\n}\nnumList.add(tmp);\n}\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nvector<vector<int>> numMatrix;\nfor (int i = 0; i < n; i++) {\nvector<int> tmp;\nfor (int j = 0; j < n; j++) {\ntmp.push_back(0);\n}\nnumMatrix.push_back(tmp);\n}\n}\n</code></pre> space_complexity.py<pre><code>\"\"\" \u5e73\u65b9\u9636 \"\"\"\ndef quadratic(n):\n# \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nnum_matrix = [[0] * n for _ in range(n)]\n</code></pre> space_complexity.go<pre><code>/* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nnumMatrix := make([][]int, n)\nfor i := 0; i < n; i++ {\nnumMatrix[i] = make([]int, n)\n}\n}\n</code></pre> space_complexity.js<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numMatrix = Array(n).fill(null).map(() => Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i < n; i++) {\nconst tmp = [];\nfor (let j = 0; j < n; j++) {\ntmp.push(0);\n}\nnumList.push(tmp);\n}\n}\n</code></pre> space_complexity.ts<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numMatrix = Array(n).fill(null).map(() => Array(n).fill(null));\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nconst numList = [];\nfor (let i = 0; i < n; i++) {\nconst tmp = [];\nfor (let j = 0; j < n; j++) {\ntmp.push(0);\n}\nnumList.push(tmp);\n}\n}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{spaceQuadratic}\n</code></pre> space_complexity.cs<pre><code>/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n)\n{\n// \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\nint[,] numMatrix = new int[n, n];\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nList<List<int>> numList = new();\nfor (int i = 0; i < n; i++)\n{\nList<int> tmp = new();\nfor (int j = 0; j < n; j++)\n{\ntmp.Add(0);\n}\nnumList.Add(tmp);\n}\n}\n</code></pre> space_complexity.swift<pre><code>/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nlet numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n</code></pre> space_complexity.zig<pre><code>// \u5e73\u65b9\u9636\nfn quadratic(n: i32) !void {\n// \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\nvar nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\ndefer nodes.deinit();\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\nvar tmp = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer tmp.deinit();\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\ntry tmp.append(0);\n}\ntry nodes.append(tmp);\n}\n}\n</code></pre> <p>\u5728\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u540c\u65f6\u5b58\u5728 \\(n\\) \u4e2a\u672a\u8fd4\u56de\u7684 <code>algorithm()</code> \uff0c\u5e76\u4e14\u6bcf\u4e2a\u51fd\u6570\u4e2d\u90fd\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u957f\u5ea6\u5206\u522b\u4e3a \\(n, n-1, n-2, ..., 2, 1\\) \uff0c\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u603b\u4f53\u4f7f\u7528 \\(O(n^2)\\) \u7a7a\u95f4\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig space_complexity.java<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0) return 0;\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nint[] nums = new int[n];\nSystem.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\nif (n <= 0) return 0;\nvector<int> nums(n);\ncout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.py<pre><code>\"\"\" \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 \"\"\"\ndef quadratic_recur(n):\nif n <= 0: return 0\n# \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nnums = [0] * n\nreturn quadratic_recur(n - 1)\n</code></pre> space_complexity.go<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\nif n <= 0 {\nreturn 0\n}\nnums := make([]int, n)\nfmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\nreturn spaceQuadraticRecur(n - 1)\n}\n</code></pre> space_complexity.js<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\nif (n <= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.ts<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\nif (n <= 0) return 0;\nconst nums = new Array(n);\nconsole.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{spaceQuadraticRecur}\n</code></pre> space_complexity.cs<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n)\n{\nif (n <= 0) return 0;\nint[] nums = new int[n];\nConsole.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\nreturn quadraticRecur(n - 1);\n}\n</code></pre> space_complexity.swift<pre><code>/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\nif n <= 0 {\nreturn 0\n}\n// \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\nlet nums = Array(repeating: 0, count: n)\nprint(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\nreturn quadraticRecur(n: n - 1)\n}\n</code></pre> space_complexity.zig<pre><code>// \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\nif (n <= 0) return 0;\nvar nums = [_]i32{0}**n;\nstd.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\nreturn quadraticRecur(n - 1);\n}\n</code></pre> <p></p> <p> Fig. \u9012\u5f52\u51fd\u6570\u4ea7\u751f\u7684\u5e73\u65b9\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"<p>\u6307\u6570\u9636\u5e38\u89c1\u4e8e\u4e8c\u53c9\u6811\u3002\u9ad8\u5ea6\u4e3a \\(n\\) \u7684\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\u7684\u7ed3\u70b9\u6570\u91cf\u4e3a \\(2^n - 1\\) \uff0c\u4f7f\u7528 \\(O(2^n)\\) \u7a7a\u95f4\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig space_complexity.java<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\nif (n == 0) return null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.cpp<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode* buildTree(int n) {\nif (n == 0) return nullptr;\nTreeNode* root = new TreeNode(0);\nroot->left = buildTree(n - 1);\nroot->right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.py<pre><code>\"\"\" \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 \"\"\"\ndef build_tree(n):\nif n == 0: return None\nroot = TreeNode(0)\nroot.left = build_tree(n - 1)\nroot.right = build_tree(n - 1)\nreturn root\n</code></pre> space_complexity.go<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *treeNode {\nif n == 0 {\nreturn nil\n}\nroot := newTreeNode(0)\nroot.left = buildTree(n - 1)\nroot.right = buildTree(n - 1)\nreturn root\n}\n</code></pre> space_complexity.js<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.ts<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\nif (n === 0) return null;\nconst root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.c<pre><code>[class]{}-[func]{buildTree}\n</code></pre> space_complexity.cs<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n)\n{\nif (n == 0) return null;\nTreeNode root = new TreeNode(0);\nroot.left = buildTree(n - 1);\nroot.right = buildTree(n - 1);\nreturn root;\n}\n</code></pre> space_complexity.swift<pre><code>/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\nif n == 0 {\nreturn nil\n}\nlet root = TreeNode(x: 0)\nroot.left = buildTree(n: n - 1)\nroot.right = buildTree(n: n - 1)\nreturn root\n}\n</code></pre> space_complexity.zig<pre><code>// \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\nfn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {\nif (n == 0) return null;\nconst root = try mem_allocator.create(inc.TreeNode(i32));\nroot.init(0);\nroot.left = try buildTree(mem_allocator, n - 1);\nroot.right = try buildTree(mem_allocator, n - 1);\nreturn root;\n}\n</code></pre> <p></p> <p> Fig. \u6ee1\u4e8c\u53c9\u6811\u4ea7\u751f\u7684\u6307\u6570\u9636\u7a7a\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/space_complexity/#olog-n","title":"\u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"<p>\u5bf9\u6570\u9636\u5e38\u89c1\u4e8e\u5206\u6cbb\u7b97\u6cd5\u3001\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u3002</p> <p>\u4f8b\u5982\u300c\u5f52\u5e76\u6392\u5e8f\u300d\uff0c\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6570\u7ec4\u53ef\u4ee5\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002</p> <p>\u518d\u4f8b\u5982\u300c\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u300d\uff0c\u8f93\u5165\u4efb\u610f\u6b63\u6574\u6570 \\(n\\) \uff0c\u5b83\u7684\u4f4d\u6570\u4e3a \\(\\log_{10} n\\) \uff0c\u5373\u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a \\(\\log_{10} n\\) \uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log_{10} n) = O(\\log n)\\) \u3002</p>"},{"location":"chapter_computational_complexity/space_time_tradeoff/","title":"2.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\u591f\u8fbe\u5230\u6700\u4f18\uff0c\u800c\u5b9e\u9645\u4e0a\uff0c\u540c\u65f6\u4f18\u5316\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u662f\u975e\u5e38\u56f0\u96be\u7684\u3002</p> <p>\u964d\u4f4e\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u5f80\u5f80\u662f\u4ee5\u63d0\u5347\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a\u4ee3\u4ef7\u7684\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u6211\u4eec\u628a\u727a\u7272\u5185\u5b58\u7a7a\u95f4\u6765\u63d0\u5347\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u7684\u601d\u8def\u79f0\u4e3a\u300c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u300d\uff1b\u53cd\u4e4b\uff0c\u79f0\u4e4b\u4e3a\u300c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u300d\u3002\u9009\u62e9\u54ea\u79cd\u601d\u8def\u53d6\u51b3\u4e8e\u6211\u4eec\u66f4\u770b\u91cd\u54ea\u4e2a\u65b9\u9762\u3002</p> <p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u90fd\u662f\u6bd4\u7a7a\u95f4\u66f4\u5b9d\u8d35\u7684\uff0c\u53ea\u8981\u7a7a\u95f4\u590d\u6742\u5ea6\u4e0d\u8981\u592a\u79bb\u8c31\u3001\u80fd\u63a5\u53d7\u5c31\u884c\uff0c\u56e0\u6b64\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u6700\u4e3a\u5e38\u7528\u3002</p>"},{"location":"chapter_computational_complexity/space_time_tradeoff/#241","title":"2.4.1. \u00a0 \u793a\u4f8b\u9898\u76ee *","text":"<p>\u4ee5 LeetCode \u5168\u7ad9\u7b2c\u4e00\u9898 \u4e24\u6570\u4e4b\u548c \u4e3a\u4f8b\u3002</p> <p>\u4e24\u6570\u4e4b\u548c</p> <p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>nums</code> \u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c <code>target</code> \uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa\u201c\u548c\u201d\u4e3a\u76ee\u6807\u503c <code>target</code> \u7684\u90a3\u4e24\u4e2a\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002</p> <p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\u3002\u4f46\u662f\uff0c\u6570\u7ec4\u4e2d\u540c\u4e00\u4e2a\u5143\u7d20\u5728\u7b54\u6848\u91cc\u4e0d\u80fd\u91cd\u590d\u51fa\u73b0\u3002</p> <p>\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002</p> <p>\u300c\u66b4\u529b\u679a\u4e3e\u300d\u548c\u300c\u8f85\u52a9\u54c8\u5e0c\u8868\u300d\u5206\u522b\u4e3a \u7a7a\u95f4\u6700\u4f18 \u548c \u65f6\u95f4\u6700\u4f18 \u7684\u4e24\u79cd\u89e3\u6cd5\u3002\u672c\u7740\u65f6\u95f4\u6bd4\u7a7a\u95f4\u66f4\u5b9d\u8d35\u7684\u539f\u5219\uff0c\u540e\u8005\u662f\u672c\u9898\u7684\u6700\u4f73\u89e3\u6cd5\u3002</p>"},{"location":"chapter_computational_complexity/space_time_tradeoff/#_1","title":"\u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(N^2)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff0c\u5c5e\u4e8e\u300c\u65f6\u95f4\u6362\u7a7a\u95f4\u300d\u3002</p> <p>\u867d\u7136\u4ec5\u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff0c\u4f46\u8fd0\u884c\u901f\u5ea6\u8fc7\u6162\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig leetcode_two_sum.java<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target) {\nint size = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn new int[0];\n}\n</code></pre> leetcode_two_sum.cpp<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nvector<int> twoSumBruteForce(vector<int>& nums, int target) {\nint size = nums.size();\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++) {\nfor (int j = i + 1; j < size; j++) {\nif (nums[i] + nums[j] == target)\nreturn { i, j };\n}\n}\nreturn {};\n}\n</code></pre> leetcode_two_sum.py<pre><code>\"\"\" \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e \"\"\"\ndef two_sum_brute_force(nums: List[int], target: int) -> List[int]:\n# \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i in range(len(nums) - 1):\nfor j in range(i + 1, len(nums)):\nif nums[i] + nums[j] == target:\nreturn i, j\nreturn []\n</code></pre> leetcode_two_sum.go<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums []int, target int) []int {\nsize := len(nums)\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i := 0; i < size-1; i++ {\nfor j := i + 1; i < size; j++ {\nif nums[i]+nums[j] == target {\nreturn []int{i, j}\n}\n}\n}\nreturn nil\n}\n</code></pre> leetcode_two_sum.js<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums, target) {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i < n; i++) {\nfor (let j = i + 1; j < n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n}\n</code></pre> leetcode_two_sum.ts<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\nconst n = nums.length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (let i = 0; i < n; i++) {\nfor (let j = i + 1; j < n; j++) {\nif (nums[i] + nums[j] === target) {\nreturn [i, j];\n}\n}\n}\nreturn [];\n};\n</code></pre> leetcode_two_sum.c<pre><code>[class]{}-[func]{twoSumBruteForce}\n</code></pre> leetcode_two_sum.cs<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nint[] twoSumBruteForce(int[] nums, int target)\n{\nint size = nums.Length;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor (int i = 0; i < size - 1; i++)\n{\nfor (int j = i + 1; j < size; j++)\n{\nif (nums[i] + nums[j] == target)\nreturn new int[] { i, j };\n}\n}\nreturn new int[0];\n}\n</code></pre> leetcode_two_sum.swift<pre><code>/* \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nfor i in nums.indices.dropLast() {\nfor j in nums.indices.dropFirst(i + 1) {\nif nums[i] + nums[j] == target {\nreturn [i, j]\n}\n}\n}\nreturn [0]\n}\n</code></pre> leetcode_two_sum.zig<pre><code>// \u65b9\u6cd5\u4e00\uff1a\u66b4\u529b\u679a\u4e3e\nfn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {\nvar size: usize = nums.len;\nvar i: usize = 0;\n// \u4e24\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n^2)\nwhile (i < size - 1) : (i += 1) {\nvar j = i + 1;\nwhile (j < size) : (j += 1) {\nif (nums[i] + nums[j] == target) {\nreturn [_]i32{@intCast(i32, i), @intCast(i32, j)};\n}\n}\n}\nreturn null;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/space_time_tradeoff/#_2","title":"\u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(N)\\) \uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(N)\\) \uff0c\u5c5e\u4e8e\u300c\u7a7a\u95f4\u6362\u65f6\u95f4\u300d\u3002</p> <p>\u501f\u52a9\u8f85\u52a9\u54c8\u5e0c\u8868 dic \uff0c\u901a\u8fc7\u4fdd\u5b58\u6570\u7ec4\u5143\u7d20\u4e0e\u7d22\u5f15\u7684\u6620\u5c04\u6765\u63d0\u5347\u7b97\u6cd5\u8fd0\u884c\u901f\u5ea6\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig leetcode_two_sum.java<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target) {\nint size = nums.length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nMap<Integer, Integer> dic = new HashMap<>();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.containsKey(target - nums[i])) {\nreturn new int[] { dic.get(target - nums[i]), i };\n}\ndic.put(nums[i], i);\n}\nreturn new int[0];\n}\n</code></pre> leetcode_two_sum.cpp<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nvector<int> twoSumHashTable(vector<int>& nums, int target) {\nint size = nums.size();\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nunordered_map<int, int> dic;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++) {\nif (dic.find(target - nums[i]) != dic.end()) {\nreturn { dic[target - nums[i]], i };\n}\ndic.emplace(nums[i], i);\n}\nreturn {};\n}\n</code></pre> leetcode_two_sum.py<pre><code>\"\"\" \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 \"\"\"\ndef two_sum_hash_table(nums: List[int], target: int) -> List[int]:\n# \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\ndic = {}\n# \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in range(len(nums)):\nif target - nums[i] in dic:\nreturn dic[target - nums[i]], i\ndic[nums[i]] = i\nreturn []\n</code></pre> leetcode_two_sum.go<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums []int, target int) []int {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nhashTable := map[int]int{}\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor idx, val := range nums {\nif preIdx, ok := hashTable[target-val]; ok {\nreturn []int{preIdx, idx}\n}\nhashTable[val] = idx\n}\nreturn nil\n}\n</code></pre> leetcode_two_sum.js<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums, target) {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nlet m = {};\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i < nums.length; i++) {\nif (m[nums[i]] !== undefined) {\nreturn [m[nums[i]], i];\n} else {\nm[target - nums[i]] = i;\n}\n}\nreturn [];\n}\n</code></pre> leetcode_two_sum.ts<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nlet m: Map<number, number> = new Map();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (let i = 0; i < nums.length; i++) {\nlet index = m.get(nums[i]);\nif (index !== undefined) {\nreturn [index, i];\n} else {\nm.set(target - nums[i], i);\n}\n}\nreturn [];\n};\n</code></pre> leetcode_two_sum.c<pre><code>[class]{}-[func]{twoSumHashTable}\n</code></pre> leetcode_two_sum.cs<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nint[] twoSumHashTable(int[] nums, int target)\n{\nint size = nums.Length;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nDictionary<int, int> dic = new();\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor (int i = 0; i < size; i++)\n{\nif (dic.ContainsKey(target - nums[i]))\n{\nreturn new int[] { dic[target - nums[i]], i };\n}\ndic.Add(nums[i], i);\n}\nreturn new int[0];\n}\n</code></pre> leetcode_two_sum.swift<pre><code>/* \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868 */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nvar dic: [Int: Int] = [:]\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nfor i in nums.indices {\nif let j = dic[target - nums[i]] {\nreturn [j, i]\n}\ndic[nums[i]] = i\n}\nreturn [0]\n}\n</code></pre> leetcode_two_sum.zig<pre><code>// \u65b9\u6cd5\u4e8c\uff1a\u8f85\u52a9\u54c8\u5e0c\u8868\nfn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {\nvar size: usize = nums.len;\n// \u8f85\u52a9\u54c8\u5e0c\u8868\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6 O(n)\nvar dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);\ndefer dic.deinit();\nvar i: usize = 0;\n// \u5355\u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nwhile (i < size) : (i += 1) {\nif (dic.contains(target - nums[i])) {\nreturn [_]i32{dic.get(target - nums[i]).?, @intCast(i32, i)};\n}\ntry dic.put(nums[i], @intCast(i32, i));\n}\nreturn null;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/summary/","title":"2.5. \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_computational_complexity/summary/#_1","title":"\u7b97\u6cd5\u6548\u7387\u8bc4\u4f30","text":"<ul> <li>\u300c\u65f6\u95f4\u6548\u7387\u300d\u548c\u300c\u7a7a\u95f4\u6548\u7387\u300d\u662f\u7b97\u6cd5\u6027\u80fd\u7684\u4e24\u4e2a\u91cd\u8981\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u3002</li> <li>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u300c\u5b9e\u9645\u6d4b\u8bd5\u300d\u6765\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\uff0c\u4f46\u96be\u4ee5\u6392\u9664\u6d4b\u8bd5\u73af\u5883\u7684\u5e72\u6270\uff0c\u5e76\u4e14\u975e\u5e38\u8017\u8d39\u8ba1\u7b97\u8d44\u6e90\u3002</li> <li>\u300c\u590d\u6742\u5ea6\u5206\u6790\u300d\u514b\u670d\u4e86\u5b9e\u9645\u6d4b\u8bd5\u7684\u5f0a\u7aef\uff0c\u5206\u6790\u7ed3\u679c\u9002\u7528\u4e8e\u6240\u6709\u8fd0\u884c\u5e73\u53f0\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f53\u73b0\u4e0d\u540c\u6570\u636e\u5927\u5c0f\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\u3002</li> </ul>"},{"location":"chapter_computational_complexity/summary/#_2","title":"\u65f6\u95f4\u590d\u6742\u5ea6","text":"<ul> <li>\u300c\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\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\u6bd4\u5982\u5728\u8f93\u5165\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\u65f6\uff0c\u65e0\u6cd5\u7cbe\u786e\u5bf9\u6bd4\u7b97\u6cd5\u6548\u7387\u7684\u4f18\u52a3\u6027\u3002</li> <li>\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u4f7f\u7528\u5927 \\(O\\) \u7b26\u53f7\u8868\u793a\uff0c\u5373\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\uff0c\u5176\u53cd\u6620\u5f53 \\(n\\) \u8d8b\u4e8e\u6b63\u65e0\u7a77\u65f6\uff0c\\(T(n)\\) \u5904\u4e8e\u4f55\u79cd\u589e\u957f\u7ea7\u522b\u3002</li> <li>\u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u7edf\u8ba1\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\uff0c\u518d\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u3002</li> <li>\u5e38\u89c1\u65f6\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n \\log n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) , \\(O(n!)\\) \u3002</li> <li>\u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f\u6052\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u4e3a\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u548c\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\uff0c\u540e\u8005\u51e0\u4e4e\u4e0d\u7528\uff0c\u56e0\u4e3a\u8f93\u5165\u6570\u636e\u9700\u8981\u6ee1\u8db3\u82db\u523b\u7684\u6761\u4ef6\u624d\u80fd\u8fbe\u5230\u6700\u4f73\u60c5\u51b5\u3002</li> <li>\u300c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u53ef\u4ee5\u53cd\u6620\u5728\u968f\u673a\u6570\u636e\u8f93\u5165\u4e0b\u7684\u7b97\u6cd5\u6548\u7387\uff0c\u6700\u8d34\u5408\u5b9e\u9645\u4f7f\u7528\u60c5\u51b5\u4e0b\u7684\u7b97\u6cd5\u6027\u80fd\u3002\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u9700\u8981\u7edf\u8ba1\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\uff0c\u4ee5\u53ca\u7efc\u5408\u540e\u7684\u6570\u5b66\u671f\u671b\u3002</li> </ul>"},{"location":"chapter_computational_complexity/summary/#_3","title":"\u7a7a\u95f4\u590d\u6742\u5ea6","text":"<ul> <li> <p>\u4e0e\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5b9a\u4e49\u7c7b\u4f3c\uff0c\u300c\u7a7a\u95f4\u590d\u6742\u5ea6\u300d\u7edf\u8ba1\u7b97\u6cd5\u5360\u7528\u7a7a\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf\u3002</p> </li> <li> <p>\u7b97\u6cd5\u8fd0\u884c\u4e2d\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\u4e00\u822c\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u624d\u4f1a\u5f71\u54cd\u5230\u7a7a\u95f4\u590d\u6742\u5ea6\u3002</p> </li> <li>\u6211\u4eec\u4e00\u822c\u53ea\u5173\u5fc3\u300c\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u300d\uff0c\u5373\u7edf\u8ba1\u7b97\u6cd5\u5728\u300c\u6700\u5dee\u8f93\u5165\u6570\u636e\u300d\u548c\u300c\u6700\u5dee\u8fd0\u884c\u65f6\u95f4\u70b9\u300d\u4e0b\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002</li> <li>\u5e38\u89c1\u7a7a\u95f4\u590d\u6742\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5217\u6709 \\(O(1)\\) , \\(O(\\log n)\\) , \\(O(n)\\) , \\(O(n^2)\\) , \\(O(2^n)\\) \u3002</li> </ul>"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.2. \u00a0 \u65f6\u95f4\u590d\u6742\u5ea6","text":""},{"location":"chapter_computational_complexity/time_complexity/#221","title":"2.2.1. \u00a0 \u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4","text":"<p>\u8fd0\u884c\u65f6\u95f4\u80fd\u591f\u76f4\u89c2\u4e14\u51c6\u786e\u5730\u4f53\u73b0\u51fa\u7b97\u6cd5\u7684\u6548\u7387\u6c34\u5e73\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981 \u51c6\u786e\u9884\u4f30\u4e00\u6bb5\u4ee3\u7801\u7684\u8fd0\u884c\u65f6\u95f4 \uff0c\u8be5\u5982\u4f55\u505a\u5462\uff1f</p> <ol> <li>\u9996\u5148\u9700\u8981 \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\u90fd\u4f1a\u5f71\u54cd\u5230\u4ee3\u7801\u7684\u8fd0\u884c\u6548\u7387\u3002</li> <li>\u8bc4\u4f30 \u5404\u79cd\u8ba1\u7b97\u64cd\u4f5c\u7684\u6240\u9700\u8fd0\u884c\u65f6\u95f4 \uff0c\u4f8b\u5982\u52a0\u6cd5\u64cd\u4f5c <code>+</code> \u9700\u8981 1 ns \uff0c\u4e58\u6cd5\u64cd\u4f5c <code>*</code> \u9700\u8981 10 ns \uff0c\u6253\u5370\u64cd\u4f5c\u9700\u8981 5 ns \u7b49\u3002</li> <li>\u6839\u636e\u4ee3\u7801 \u7edf\u8ba1\u6240\u6709\u8ba1\u7b97\u64cd\u4f5c\u7684\u6570\u91cf \uff0c\u5e76\u5c06\u6240\u6709\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u6c42\u548c\uff0c\u5373\u53ef\u5f97\u5230\u8fd0\u884c\u65f6\u95f4\u3002</li> </ol> <p>\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u6839\u636e\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5f97\u5230\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u4e3a \\(6n + 12\\) ns \u3002</p> \\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <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++\ncout << 0 << endl; // 5 ns\n}\n}\n</code></pre> <pre><code># \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\ndef algorithm(n):\na = 2 # 1 ns\na = a + 1 # 1 ns\na = a * 2 # 10 ns\n# \u5faa\u73af n \u6b21\nfor _ in range(n): # 1 ns\nprint(0) # 5 ns\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunc algorithm(n int) {\na := 2 // 1 ns\na = a + 1 // 1 ns\na = a * 2 // 10 ns\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ { // 1 ns\nfmt.Println(a) // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nfunction algorithm(n) {\nvar a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < 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++\nprintf(\"%d\", 0); // 5 ns\n}\n}\n</code></pre> <pre><code>// \u5728\u67d0\u8fd0\u884c\u5e73\u53f0\u4e0b\nvoid algorithm(int n)\n{\nint a = 2; // 1 ns\na = a + 1; // 1 ns\na = a * 2; // 10 ns\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++)\n{ // 1 ns \uff0c\u6bcf\u8f6e\u90fd\u8981\u6267\u884c i++\nConsole.WriteLine(0); // 5 ns\n}\n}\n</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>\n</code></pre> <p>\u4f46\u5b9e\u9645\u4e0a\uff0c \u7edf\u8ba1\u7b97\u6cd5\u7684\u8fd0\u884c\u65f6\u95f4\u65e2\u4e0d\u5408\u7406\u4e5f\u4e0d\u73b0\u5b9e\u3002\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u9884\u4f30\u65f6\u95f4\u548c\u8fd0\u884c\u5e73\u53f0\u7ed1\u5b9a\uff0c\u6bd5\u7adf\u7b97\u6cd5\u9700\u8981\u8dd1\u5728\u5404\u5f0f\u5404\u6837\u7684\u5e73\u53f0\u4e4b\u4e0a\u3002\u5176\u6b21\uff0c\u6211\u4eec\u5f88\u96be\u83b7\u77e5\u6bcf\u4e00\u79cd\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\uff0c\u8fd9\u4e3a\u9884\u4f30\u8fc7\u7a0b\u5e26\u6765\u4e86\u6781\u5927\u7684\u96be\u5ea6\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#222","title":"2.2.2. \u00a0 \u7edf\u8ba1\u65f6\u95f4\u589e\u957f\u8d8b\u52bf","text":"<p>\u300c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u300d\u91c7\u53d6\u4e86\u4e0d\u540c\u7684\u505a\u6cd5\uff0c\u5176\u7edf\u8ba1\u7684\u4e0d\u662f\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u800c\u662f \u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u968f\u7740\u6570\u636e\u91cf\u53d8\u5927\u65f6\u7684\u589e\u957f\u8d8b\u52bf \u3002</p> <p>\u201c\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u201d\u8fd9\u4e2a\u6982\u5ff5\u6bd4\u8f83\u62bd\u8c61\uff0c\u6211\u4eec\u501f\u52a9\u4e00\u4e2a\u4f8b\u5b50\u6765\u7406\u89e3\u3002\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7ed9\u5b9a\u4e09\u4e2a\u7b97\u6cd5 <code>A</code> , <code>B</code> , <code>C</code> \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\u300c\u5e38\u6570\u9636\u300d\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\u6210\u7ebf\u6027\u589e\u957f\u3002\u6b64\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u88ab\u79f0\u4e3a\u300c\u7ebf\u6027\u9636\u300d\u3002</li> <li>\u7b97\u6cd5 <code>C</code> \u4e2d\u7684\u6253\u5370\u64cd\u4f5c\u9700\u8981\u5faa\u73af \\(1000000\\) \u6b21\uff0c\u4f46\u8fd0\u884c\u65f6\u95f4\u4ecd\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\u3002\u56e0\u6b64 <code>C</code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c <code>A</code> \u76f8\u540c\uff0c\u4ecd\u4e3a\u300c\u5e38\u6570\u9636\u300d\u3002</li> </ul> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nSystem.out.println(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nSystem.out.println(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nSystem.out.println(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\ncout << 0 << endl;\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\ncout << 0 << endl;\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\ncout << 0 << endl;\n}\n}\n</code></pre> <pre><code># \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_A(n):\nprint(0)\n# \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\ndef algorithm_B(n):\nfor _ in range(n):\nprint(0)\n# \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\ndef algorithm_C(n):\nfor _ in range(1000000):\nprint(0)\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_A(n int) {\nfmt.Println(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithm_B(n int) {\nfor i := 0; i < n; i++ {\nfmt.Println(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithm_C(n int) {\nfor i := 0; i < 1000000; i++ {\nfmt.Println(0)\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n) {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n) {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n) {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_A(n: number): void {\nconsole.log(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunction algorithm_B(n: number): void {\nfor (let i = 0; i < n; i++) {\nconsole.log(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunction algorithm_C(n: number): void {\nfor (let i = 0; i < 1000000; i++) {\nconsole.log(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n) {\nprintf(\"%d\", 0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n) {\nfor (int i = 0; i < n; i++) {\nprintf(\"%d\", 0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n) {\nfor (int i = 0; i < 1000000; i++) {\nprintf(\"%d\", 0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_A(int n)\n{\nConsole.WriteLine(0);\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nvoid algorithm_B(int n)\n{\nfor (int i = 0; i < n; i++)\n{\nConsole.WriteLine(0);\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nvoid algorithm_C(int n)\n{\nfor (int i = 0; i < 1000000; i++)\n{\nConsole.WriteLine(0);\n}\n}\n</code></pre> <pre><code>// \u7b97\u6cd5 A \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmA(_ n: Int) {\nprint(0)\n}\n// \u7b97\u6cd5 B \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u7ebf\u6027\u9636\nfunc algorithmB(_ n: Int) {\nfor _ in 0 ..< n {\nprint(0)\n}\n}\n// \u7b97\u6cd5 C \u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u5e38\u6570\u9636\nfunc algorithmC(_ n: Int) {\nfor _ in 0 ..< 1000000 {\nprint(0)\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p></p> <p> Fig. \u7b97\u6cd5 A, B, C \u7684\u65f6\u95f4\u589e\u957f\u8d8b\u52bf </p> <p>\u76f8\u6bd4\u76f4\u63a5\u7edf\u8ba1\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u7684\u505a\u6cd5\u6709\u4ec0\u4e48\u597d\u5904\u5462\uff1f\u4ee5\u53ca\u6709\u4ec0\u4e48\u4e0d\u8db3\uff1f</p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u6709\u6548\u8bc4\u4f30\u7b97\u6cd5\u6548\u7387\u3002\u7b97\u6cd5 <code>B</code> \u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u662f\u7ebf\u6027\u7684\uff0c\u5728 \\(n > 1\\) \u65f6\u6162\u4e8e\u7b97\u6cd5 <code>A</code> \uff0c\u5728 \\(n > 1000000\\) \u65f6\u6162\u4e8e\u7b97\u6cd5 <code>C</code> \u3002\u5b9e\u8d28\u4e0a\uff0c\u53ea\u8981\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u8db3\u591f\u5927\uff0c\u590d\u6742\u5ea6\u4e3a\u300c\u5e38\u6570\u9636\u300d\u7684\u7b97\u6cd5\u4e00\u5b9a\u4f18\u4e8e\u300c\u7ebf\u6027\u9636\u300d\u7684\u7b97\u6cd5\uff0c\u8fd9\u4e5f\u6b63\u662f\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u7684\u542b\u4e49\u3002</p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u63a8\u7b97\u65b9\u6cd5\u66f4\u52a0\u7b80\u4fbf\u3002\u5728\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7edf\u8ba1\u300c\u8ba1\u7b97\u64cd\u4f5c\u7684\u8fd0\u884c\u65f6\u95f4\u300d\u7b80\u5316\u4e3a\u7edf\u8ba1\u300c\u8ba1\u7b97\u64cd\u4f5c\u7684\u6570\u91cf\u300d\uff0c\u8fd9\u662f\u56e0\u4e3a\uff0c\u65e0\u8bba\u662f\u8fd0\u884c\u5e73\u53f0\u8fd8\u662f\u8ba1\u7b97\u64cd\u4f5c\u7c7b\u578b\uff0c\u90fd\u4e0e\u7b97\u6cd5\u8fd0\u884c\u65f6\u95f4\u7684\u589e\u957f\u8d8b\u52bf\u65e0\u5173\u3002\u56e0\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u6240\u6709\u8ba1\u7b97\u64cd\u4f5c\u7684\u6267\u884c\u65f6\u95f4\u7edf\u4e00\u770b\u4f5c\u662f\u76f8\u540c\u7684\u201c\u5355\u4f4d\u65f6\u95f4\u201d\uff0c\u8fd9\u6837\u7684\u7b80\u5316\u505a\u6cd5\u5927\u5927\u964d\u4f4e\u4e86\u4f30\u7b97\u96be\u5ea6\u3002</p> <p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027\u3002\u6bd4\u5982\uff0c\u867d\u7136\u7b97\u6cd5 <code>A</code> \u548c <code>C</code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u76f8\u540c\uff0c\u4f46\u662f\u5b9e\u9645\u7684\u8fd0\u884c\u65f6\u95f4\u6709\u975e\u5e38\u5927\u7684\u5dee\u522b\u3002\u518d\u6bd4\u5982\uff0c\u867d\u7136\u7b97\u6cd5 <code>B</code> \u6bd4 <code>C</code> \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8981\u66f4\u9ad8\uff0c\u4f46\u5728\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u6bd4\u8f83\u5c0f\u65f6\uff0c\u7b97\u6cd5 <code>B</code> \u662f\u8981\u660e\u663e\u4f18\u4e8e\u7b97\u6cd5 <code>C</code> \u7684\u3002\u5bf9\u4e8e\u4ee5\u4e0a\u60c5\u51b5\uff0c\u6211\u4eec\u5f88\u96be\u4ec5\u51ed\u65f6\u95f4\u590d\u6742\u5ea6\u6765\u5224\u5b9a\u7b97\u6cd5\u6548\u7387\u9ad8\u4f4e\u3002\u7136\u800c\uff0c\u5373\u4f7f\u5b58\u5728\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba1\u7b97\u590d\u6742\u5ea6\u4ecd\u7136\u662f\u8bc4\u5224\u7b97\u6cd5\u6548\u7387\u7684\u6700\u6709\u6548\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#223","title":"2.2.3. \u00a0 \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c","text":"<p>\u8bbe\u7b97\u6cd5\u300c\u8ba1\u7b97\u64cd\u4f5c\u6570\u91cf\u300d\u4e3a \\(T(n)\\) \uff0c\u5176\u662f\u4e00\u4e2a\u5173\u4e8e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u7b97\u6cd5\u7684\u64cd\u4f5c\u6570\u91cf\u4e3a</p> \\[ T(n) = 3 + 2n \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <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\ncout << 0 << endl; // +1\n}\n}\n</code></pre> <pre><code>def algorithm(n):\na = 1 # +1\na = a + 1 # +1\na = a * 2 # +1\n# \u5faa\u73af n \u6b21\nfor i in range(n): # +1\nprint(0) # +1\n</code></pre> <pre><code>func algorithm(n int) {\na := 1 // +1\na = a + 1 // +1\na = a * 2 // +1\n// \u5faa\u73af n \u6b21\nfor i := 0; i < n; i++ { // +1\nfmt.Println(a) // +1\n}\n}\n</code></pre> <pre><code>function algorithm(n){\nvar a = 1; // +1\na += 1; // +1\na *= 2; // +1\n// \u5faa\u73af n \u6b21\nfor(let i = 0; i < 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\nprintf(\"%d\", 0); // +1\n}\n} </code></pre> <pre><code>void algorithm(int n) {\nint a = 1; // +1\na = a + 1; // +1\na = a * 2; // +1\n// \u5faa\u73af n \u6b21\nfor (int i = 0; i < n; i++) { // +1\uff08\u6bcf\u8f6e\u90fd\u6267\u884c i ++\uff09\nConsole.WriteLine(0); // +1\n}\n}\n</code></pre> <pre><code>func algorithm(n: Int) {\nvar a = 1 // +1\na = a + 1 // +1\na = a * 2 // +1\n// \u5faa\u73af n \u6b21\nfor _ in 0 ..< n { // +1\nprint(0) // +1\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\\(T(n)\\) \u662f\u4e2a\u4e00\u6b21\u51fd\u6570\uff0c\u8bf4\u660e\u65f6\u95f4\u589e\u957f\u8d8b\u52bf\u662f\u7ebf\u6027\u7684\uff0c\u56e0\u6b64\u6613\u5f97\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\u88ab\u79f0\u4e3a\u300c\u5927 \\(O\\) \u8bb0\u53f7 Big-\\(O\\) Notation\u300d\uff0c\u4ee3\u8868\u51fd\u6570 \\(T(n)\\) \u7684\u300c\u6e10\u8fd1\u4e0a\u754c asymptotic upper bound\u300d\u3002</p> <p>\u6211\u4eec\u8981\u63a8\u7b97\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u672c\u8d28\u4e0a\u662f\u5728\u8ba1\u7b97\u300c\u64cd\u4f5c\u6570\u91cf\u51fd\u6570 \\(T(n)\\) \u300d\u7684\u6e10\u8fd1\u4e0a\u754c\u3002\u4e0b\u9762\u6211\u4eec\u5148\u6765\u770b\u770b\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\u7684\u6570\u5b66\u5b9a\u4e49\u3002</p> <p>\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c</p> <p>\u82e5\u5b58\u5728\u6b63\u5b9e\u6570 \\(c\\) \u548c\u5b9e\u6570 \\(n_0\\) \uff0c\u4f7f\u5f97\u5bf9\u4e8e\u6240\u6709\u7684 \\(n > n_0\\) \uff0c\u5747\u6709 $$ T(n) \\leq c \\cdot f(n) $$ \u5219\u53ef\u8ba4\u4e3a \\(f(n)\\) \u7ed9\u51fa\u4e86 \\(T(n)\\) \u7684\u4e00\u4e2a\u6e10\u8fd1\u4e0a\u754c\uff0c\u8bb0\u4e3a $$ T(n) = O(f(n)) $$</p> <p></p> <p> Fig. \u51fd\u6570\u7684\u6e10\u8fd1\u4e0a\u754c </p> <p>\u672c\u8d28\u4e0a\u770b\uff0c\u8ba1\u7b97\u6e10\u8fd1\u4e0a\u754c\u5c31\u662f\u5728\u627e\u4e00\u4e2a\u51fd\u6570 \\(f(n)\\) \uff0c\u4f7f\u5f97\u5728 \\(n\\) \u8d8b\u5411\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\\(T(n)\\) \u548c \\(f(n)\\) \u5904\u4e8e\u76f8\u540c\u7684\u589e\u957f\u7ea7\u522b\uff08\u4ec5\u76f8\u5dee\u4e00\u4e2a\u5e38\u6570\u9879 \\(c\\) \u7684\u500d\u6570\uff09\u3002</p> <p>Tip</p> <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\u65e0\u9700\u62c5\u5fc3\uff0c\u56e0\u4e3a\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u6211\u4eec\u53ea\u9700\u8981\u4f1a\u63a8\u7b97\u5373\u53ef\uff0c\u6570\u5b66\u610f\u4e49\u53ef\u4ee5\u6162\u6162\u9886\u609f\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#224","title":"2.2.4. \u00a0 \u63a8\u7b97\u65b9\u6cd5","text":"<p>\u63a8\u7b97\u51fa \\(f(n)\\) \u540e\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(f(n))\\) \u3002\u90a3\u4e48\uff0c\u5982\u4f55\u6765\u786e\u5b9a\u6e10\u8fd1\u4e0a\u754c \\(f(n)\\) \u5462\uff1f\u603b\u4f53\u5206\u4e3a\u4e24\u6b65\uff0c\u9996\u5148\u300c\u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf\u300d\uff0c\u7136\u540e\u300c\u5224\u65ad\u6e10\u8fd1\u4e0a\u754c\u300d\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#1","title":"1) \u7edf\u8ba1\u64cd\u4f5c\u6570\u91cf","text":"<p>\u5bf9\u7740\u4ee3\u7801\uff0c\u4ece\u4e0a\u5230\u4e0b\u4e00\u884c\u4e00\u884c\u5730\u8ba1\u6570\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\u5077\u61d2\u6280\u5de7\uff1a</p> <ol> <li>\u8df3\u8fc7\u6570\u91cf\u4e0e \\(n\\) \u65e0\u5173\u7684\u64cd\u4f5c\u3002\u56e0\u4e3a\u4ed6\u4eec\u90fd\u662f \\(T(n)\\) \u4e2d\u7684\u5e38\u6570\u9879\uff0c\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\u3001\u2026\u2026\uff0c\u90fd\u53ef\u4ee5\u5316\u7b80\u8bb0\u4e3a \\(n\\) \u6b21\uff0c\u56e0\u4e3a \\(n\\) \u524d\u9762\u7684\u7cfb\u6570\u5bf9\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u4e0d\u4ea7\u751f\u5f71\u54cd\u3002</li> <li>\u5faa\u73af\u5d4c\u5957\u65f6\u4f7f\u7528\u4e58\u6cd5\u3002\u603b\u64cd\u4f5c\u6570\u91cf\u7b49\u4e8e\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u64cd\u4f5c\u6570\u91cf\u4e4b\u79ef\uff0c\u6bcf\u4e00\u5c42\u5faa\u73af\u4f9d\u7136\u53ef\u4ee5\u5206\u522b\u5957\u7528\u4e0a\u8ff0 <code>1.</code> \u548c <code>2.</code> \u6280\u5de7\u3002</li> </ol> <p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u4f7f\u7528\u4e0a\u8ff0\u6280\u5de7\u524d\u3001\u540e\u7684\u7edf\u8ba1\u7ed3\u679c\u3002</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} \\] <p>\u6700\u7ec8\uff0c\u4e24\u8005\u90fd\u80fd\u63a8\u51fa\u76f8\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7ed3\u679c\uff0c\u5373 \\(O(n^2)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <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++) {\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>def algorithm(n):\na = 1 # +0\uff08\u6280\u5de7 1\uff09\na = a + n # +0\uff08\u6280\u5de7 1\uff09\n# +n\uff08\u6280\u5de7 2\uff09\nfor i in range(5 * n + 1):\nprint(0)\n# +n*n\uff08\u6280\u5de7 3\uff09\nfor i in range(2 * n):\nfor j in range(n + 1):\nprint(0)\n</code></pre> <pre><code>func algorithm(n int) {\na := 1 // +0\uff08\u6280\u5de7 1\uff09\na = a + n // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor i := 0; i < 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>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++) {\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>void algorithm(int n)\n{\nint a = 1; // +0\uff08\u6280\u5de7 1\uff09\na = a + n; // +0\uff08\u6280\u5de7 1\uff09\n// +n\uff08\u6280\u5de7 2\uff09\nfor (int i = 0; i < 5 * n + 1; i++)\n{\nConsole.WriteLine(0);\n}\n// +n*n\uff08\u6280\u5de7 3\uff09\nfor (int i = 0; i < 2 * n; i++)\n{\nfor (int j = 0; j < n + 1; j++)\n{\nConsole.WriteLine(0);\n}\n}\n}\n</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>\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#2","title":"2) \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\u5904\u4e8e\u4e3b\u5bfc\u4f5c\u7528\uff0c\u5176\u5b83\u9879\u7684\u5f71\u54cd\u90fd\u53ef\u4ee5\u88ab\u5ffd\u7565\u3002</p> <p>\u4ee5\u4e0b\u8868\u683c\u7ed9\u51fa\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u5176\u4e2d\u6709\u4e00\u4e9b\u5938\u5f20\u7684\u503c\uff0c\u662f\u60f3\u8981\u5411\u5927\u5bb6\u5f3a\u8c03 \u7cfb\u6570\u65e0\u6cd5\u64bc\u52a8\u9636\u6570 \u8fd9\u4e00\u7ed3\u8bba\u3002\u5728 \\(n\\) \u8d8b\u4e8e\u65e0\u7a77\u5927\u65f6\uff0c\u8fd9\u4e9b\u5e38\u6570\u90fd\u662f\u201c\u6d6e\u4e91\u201d\u3002</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/#225","title":"2.2.5. \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\u6709\uff08\u4ece\u4f4e\u5230\u9ad8\u6392\u5217\uff09</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> Fig. \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u5e38\u89c1\u7c7b\u578b </p> <p>Tip</p> <p>\u90e8\u5206\u793a\u4f8b\u4ee3\u7801\u9700\u8981\u4e00\u4e9b\u524d\u7f6e\u77e5\u8bc6\uff0c\u5305\u62ec\u6570\u7ec4\u3001\u9012\u5f52\u7b97\u6cd5\u7b49\u3002\u5982\u679c\u9047\u5230\u770b\u4e0d\u61c2\u7684\u5730\u65b9\u65e0\u9700\u62c5\u5fc3\uff0c\u53ef\u4ee5\u5728\u5b66\u4e60\u5b8c\u540e\u9762\u7ae0\u8282\u540e\u518d\u6765\u590d\u4e60\uff0c\u73b0\u9636\u6bb5\u5148\u805a\u7126\u5728\u7406\u89e3\u65f6\u95f4\u590d\u6742\u5ea6\u542b\u4e49\u548c\u63a8\u7b97\u65b9\u6cd5\u4e0a\u3002</p>"},{"location":"chapter_computational_complexity/time_complexity/#o1","title":"\u5e38\u6570\u9636 \\(O(1)\\)","text":"<p>\u5e38\u6570\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u4e0e\u8f93\u5165\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u5373\u4e0d\u968f\u7740 \\(n\\) \u7684\u53d8\u5316\u800c\u53d8\u5316\u3002</p> <p>\u5bf9\u4e8e\u4ee5\u4e0b\u7b97\u6cd5\uff0c\u65e0\u8bba\u64cd\u4f5c\u6570\u91cf <code>size</code> \u6709\u591a\u5927\uff0c\u53ea\u8981\u4e0e\u6570\u636e\u5927\u5c0f \\(n\\) \u65e0\u5173\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u5c31\u4ecd\u4e3a \\(O(1)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n) {\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u5e38\u6570\u9636 \"\"\"\ndef constant(n):\ncount = 0\nsize = 100000\nfor _ in range(size):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\ncount := 0\nsize := 100000\nfor i := 0; i < size; i++ {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e38\u6570\u9636 */\nfunction constant(n) {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i < size; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\nlet count = 0;\nconst size = 100000;\nfor (let i = 0; i < size; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{constant}\n</code></pre> time_complexity.cs<pre><code>/* \u5e38\u6570\u9636 */\nint constant(int n)\n{\nint count = 0;\nint size = 100000;\nfor (int i = 0; i < size; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\nvar count = 0\nlet size = 100_000\nfor _ in 0 ..< size {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n_ = n;\nvar count: i32 = 0;\nconst size: i32 = 100_000;\nvar i: i32 = 0;\nwhile(i<size) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#on","title":"\u7ebf\u6027\u9636 \\(O(n)\\)","text":"<p>\u7ebf\u6027\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u8f93\u5165\u6570\u636e\u5927\u5c0f\u6210\u7ebf\u6027\u7ea7\u522b\u589e\u957f\u3002\u7ebf\u6027\u9636\u5e38\u51fa\u73b0\u4e8e\u5355\u5c42\u5faa\u73af\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n) {\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u7ebf\u6027\u9636 \"\"\"\ndef linear(n):\ncount = 0\nfor _ in range(n):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\ncount := 0\nfor i := 0; i < n; i++ {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\nlet count = 0;\nfor (let i = 0; i < n; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\nlet count = 0;\nfor (let i = 0; i < n; i++) count++;\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{linear}\n</code></pre> time_complexity.cs<pre><code>/* \u7ebf\u6027\u9636 */\nint linear(int n)\n{\nint count = 0;\nfor (int i = 0; i < n; i++)\ncount++;\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\nvar count = 0\nfor _ in 0 ..< n {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n</code></pre> <p>\u300c\u904d\u5386\u6570\u7ec4\u300d\u548c\u300c\u904d\u5386\u94fe\u8868\u300d\u7b49\u64cd\u4f5c\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(n)\\) \uff0c\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u7684\u957f\u5ea6\u3002</p> <p>Tip</p> <p>\u6570\u636e\u5927\u5c0f \\(n\\) \u662f\u6839\u636e\u8f93\u5165\u6570\u636e\u7684\u7c7b\u578b\u6765\u786e\u5b9a\u7684\u3002\u6bd4\u5982\uff0c\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u5c06 \\(n\\) \u770b\u4f5c\u8f93\u5165\u6570\u636e\u5927\u5c0f\uff1b\u4ee5\u4e0b\u904d\u5386\u6570\u7ec4\u793a\u4f8b\u4e2d\uff0c\u6570\u636e\u5927\u5c0f \\(n\\) \u4e3a\u6570\u7ec4\u7684\u957f\u5ea6\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int>& nums) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (int num : nums) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\ndef array_traversal(nums):\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor num in nums:\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor range nums {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (let i = 0; i < nums.length; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{arrayTraversal}\n</code></pre> time_complexity.cs<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums)\n{\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nforeach (int num in nums)\n{\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor _ in nums {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\nvar count: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\nfor (nums) |_| {\ncount += 1;\n}\nreturn count;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#on2","title":"\u5e73\u65b9\u9636 \\(O(n^2)\\)","text":"<p>\u5e73\u65b9\u9636\u7684\u64cd\u4f5c\u6570\u91cf\u76f8\u5bf9\u8f93\u5165\u6570\u636e\u5927\u5c0f\u6210\u5e73\u65b9\u7ea7\u522b\u589e\u957f\u3002\u5e73\u65b9\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\uff0c\u5916\u5c42\u5faa\u73af\u548c\u5185\u5c42\u5faa\u73af\u90fd\u4e3a \\(O(n)\\) \uff0c\u603b\u4f53\u4e3a \\(O(n^2)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u5e73\u65b9\u9636 \"\"\"\ndef quadratic(n):\ncount = 0\n# \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i in range(n):\nfor j in range(n):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\ncount := 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor i := 0; i < n; i++ {\nfor j := 0; j < n; j++ {\ncount++\n}\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\nlet count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < n; j++) {\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{quadratic}\n</code></pre> time_complexity.cs<pre><code>/* \u5e73\u65b9\u9636 */\nint quadratic(int n)\n{\nint count = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor (int i = 0; i < n; i++)\n{\nfor (int j = 0; j < n; j++)\n{\ncount++;\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\nvar count = 0\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nfor _ in 0 ..< n {\nfor _ in 0 ..< n {\ncount += 1\n}\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u5e73\u65b9\u5173\u7cfb\nwhile (i < n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j < n) : (j += 1) {\ncount += 1;\n}\n}\nreturn count;\n}\n</code></pre> <p></p> <p> Fig. \u5e38\u6570\u9636\u3001\u7ebf\u6027\u9636\u3001\u5e73\u65b9\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u4ee5\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u4e3a\u4f8b\uff0c\u5916\u5c42\u5faa\u73af \\(n - 1\\) \u6b21\uff0c\u5185\u5c42\u5faa\u73af \\(n-1, n-2, \\cdots, 2, 1\\) \u6b21\uff0c\u5e73\u5747\u4e3a \\(\\frac{n}{2}\\) \u6b21\uff0c\u56e0\u6b64\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \u3002</p> \\[ O((n - 1) \\frac{n}{2}) = O(n^2) \\] JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.length - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int>& nums) {\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.size() - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\ndef bubble_sort(nums):\ncount = 0 # \u8ba1\u6570\u5668\n# \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i in range(len(nums) - 1, 0, -1):\n# \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\ncount := 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i := len(nums) - 1; i > 0; i-- {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\ntmp := nums[j]\nnums[j] = nums[j+1]\nnums[j+1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\nlet count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> time_complexity.cs<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums)\n{\nint count = 0; // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.Length - 1; i > 0; i--)\n{\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++)\n{\nif (nums[j] > nums[j + 1])\n{\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\nvar count = 0 // \u8ba1\u6570\u5668\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i in sequence(first: nums.count - 1, next: { $0 > 0 + 1 ? $0 - 1 : nil }) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j in 0 ..< i {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\ncount += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\nvar count: i32 = 0; // \u8ba1\u6570\u5668 \n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nvar i: i32 = @intCast(i32, nums.len ) - 1;\nwhile (i > 0) : (i -= 1) {\nvar j: usize = 0;\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\ncount += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n}\n}\n}\nreturn count;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#o2n","title":"\u6307\u6570\u9636 \\(O(2^n)\\)","text":"<p>Note</p> <p>\u751f\u7269\u5b66\u79d1\u4e2d\u7684\u201c\u7ec6\u80de\u5206\u88c2\u201d\u5373\u662f\u6307\u6570\u9636\u589e\u957f\uff1a\u521d\u59cb\u72b6\u6001\u4e3a \\(1\\) \u4e2a\u7ec6\u80de\uff0c\u5206\u88c2\u4e00\u8f6e\u540e\u4e3a \\(2\\) \u4e2a\uff0c\u5206\u88c2\u4e24\u8f6e\u540e\u4e3a \\(4\\) \u4e2a\uff0c\u2026\u2026\uff0c\u5206\u88c2 \\(n\\) \u8f6e\u540e\u6709 \\(2^n\\) \u4e2a\u7ec6\u80de\u3002</p> <p>\u6307\u6570\u9636\u589e\u957f\u5f97\u975e\u5e38\u5feb\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4e00\u822c\u662f\u4e0d\u80fd\u88ab\u63a5\u53d7\u7684\u3002\u82e5\u4e00\u4e2a\u95ee\u9898\u4f7f\u7528\u300c\u66b4\u529b\u679a\u4e3e\u300d\u6c42\u89e3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f \\(O(2^n)\\) \uff0c\u90a3\u4e48\u4e00\u822c\u90fd\u9700\u8981\u4f7f\u7528\u300c\u52a8\u6001\u89c4\u5212\u300d\u6216\u300c\u8d2a\u5fc3\u7b97\u6cd5\u300d\u7b49\u7b97\u6cd5\u6765\u6c42\u89e3\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\nint count = 0, base = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++) {\nfor (int j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ndef exponential(n):\ncount, base = 0, 1\n# cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in range(n):\nfor _ in range(base):\ncount += 1\nbase *= 2\n# count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\ncount, base := 0, 1\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor i := 0; i < n; i++ {\nfor j := 0; j < base; j++ {\ncount++\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\nlet count = 0,\nbase = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\nlet count = 0,\nbase = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (let i = 0; i < n; i++) {\nfor (let j = 0; j < base; j++) {\ncount++;\n}\nbase *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{exponential}\n</code></pre> time_complexity.cs<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n)\n{\nint count = 0, bas = 1;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor (int i = 0; i < n; i++)\n{\nfor (int j = 0; j < bas; j++)\n{\ncount++;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\nvar count = 0\nvar base = 1\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nfor _ in 0 ..< n {\nfor _ in 0 ..< base {\ncount += 1\n}\nbase *= 2\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\nvar count: i32 = 0;\nvar bas: i32 = 1;\nvar i: i32 = 0;\n// cell \u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\nwhile (i < n) : (i += 1) {\nvar j: i32 = 0;\nwhile (j < bas) : (j += 1) {\ncount += 1;\n}\nbas *= 2;\n}\n// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\nreturn count;\n}\n</code></pre> <p></p> <p> Fig. \u6307\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u5728\u5b9e\u9645\u7b97\u6cd5\u4e2d\uff0c\u6307\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u4e0d\u65ad\u5730\u4e00\u5206\u4e3a\u4e8c\uff0c\u5206\u88c2 \\(n\\) \u6b21\u540e\u505c\u6b62\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\ndef exp_recur(n):\nif n == 1: return 1\nreturn exp_recur(n - 1) + exp_recur(n - 1) + 1\n</code></pre> time_complexity.go<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n-1) + expRecur(n-1) + 1\n}\n</code></pre> time_complexity.js<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{expRecur}\n</code></pre> time_complexity.cs<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n)\n{\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -> Int {\nif n == 1 {\nreturn 1\n}\nreturn expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n</code></pre> time_complexity.zig<pre><code>// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\nif (n == 1) return 1;\nreturn expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#olog-n","title":"\u5bf9\u6570\u9636 \\(O(\\log n)\\)","text":"<p>\u5bf9\u6570\u9636\u4e0e\u6307\u6570\u9636\u6b63\u597d\u76f8\u53cd\uff0c\u540e\u8005\u53cd\u6620\u201c\u6bcf\u8f6e\u589e\u52a0\u5230\u4e24\u500d\u7684\u60c5\u51b5\u201d\uff0c\u800c\u524d\u8005\u53cd\u6620\u201c\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\u7684\u60c5\u51b5\u201d\u3002\u5bf9\u6570\u9636\u4ec5\u6b21\u4e8e\u5e38\u6570\u9636\uff0c\u65f6\u95f4\u589e\u957f\u5f97\u5f88\u6162\uff0c\u662f\u7406\u60f3\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002</p> <p>\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u300c\u4e8c\u5206\u67e5\u627e\u300d\u548c\u300c\u5206\u6cbb\u7b97\u6cd5\u300d\u4e2d\uff0c\u4f53\u73b0\u201c\u4e00\u5206\u4e3a\u591a\u201d\u3001\u201c\u5316\u7e41\u4e3a\u7b80\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002</p> <p>\u8bbe\u8f93\u5165\u6570\u636e\u5927\u5c0f\u4e3a \\(n\\) \uff0c\u7531\u4e8e\u6bcf\u8f6e\u7f29\u51cf\u5230\u4e00\u534a\uff0c\u56e0\u6b64\u5faa\u73af\u6b21\u6570\u662f \\(\\log_2 n\\) \uff0c\u5373 \\(2^n\\) \u7684\u53cd\u51fd\u6570\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n) {\nint count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\ndef logarithmic(n):\ncount = 0\nwhile n > 1:\nn = n / 2\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n float64) int {\ncount := 0\nfor n > 1 {\nn = n / 2\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\nlet count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\nlet count = 0;\nwhile (n > 1) {\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{logarithmic}\n</code></pre> time_complexity.cs<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(float n)\n{\nint count = 0;\nwhile (n > 1)\n{\nn = n / 2;\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Int) -> Int {\nvar count = 0\nvar n = n\nwhile n > 1 {\nn = n / 2\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: f32) i32 {\nvar count: i32 = 0;\nvar n_var = n;\nwhile (n_var > 1)\n{\nn_var = n_var / 2;\ncount +=1;\n}\nreturn count;\n}\n</code></pre> <p></p> <p> Fig. \u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p> <p>\u4e0e\u6307\u6570\u9636\u7c7b\u4f3c\uff0c\u5bf9\u6570\u9636\u4e5f\u5e38\u51fa\u73b0\u4e8e\u9012\u5f52\u51fd\u6570\u3002\u4ee5\u4e0b\u4ee3\u7801\u5f62\u6210\u4e86\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(\\log_2 n\\) \u7684\u9012\u5f52\u6811\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\ndef log_recur(n):\nif n <= 1: return 0\nreturn log_recur(n / 2) + 1\n</code></pre> time_complexity.go<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n float64) int {\nif n <= 1 {\nreturn 0\n}\nreturn logRecur(n/2) + 1\n}\n</code></pre> time_complexity.js<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{logRecur}\n</code></pre> time_complexity.cs<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(float n)\n{\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Int) -> Int {\nif n <= 1 {\nreturn 0\n}\nreturn logRecur(n: n / 2) + 1\n}\n</code></pre> time_complexity.zig<pre><code>// \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: f32) i32 {\nif (n <= 1) return 0;\nreturn logRecur(n / 2) + 1;\n}\n</code></pre>"},{"location":"chapter_computational_complexity/time_complexity/#on-log-n","title":"\u7ebf\u6027\u5bf9\u6570\u9636 \\(O(n \\log n)\\)","text":"<p>\u7ebf\u6027\u5bf9\u6570\u9636\u5e38\u51fa\u73b0\u4e8e\u5d4c\u5957\u5faa\u73af\u4e2d\uff0c\u4e24\u5c42\u5faa\u73af\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u522b\u4e3a \\(O(\\log n)\\) \u548c \\(O(n)\\) \u3002</p> <p>\u4e3b\u6d41\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(n \\log n )\\) \uff0c\u4f8b\u5982\u5feb\u901f\u6392\u5e8f\u3001\u5f52\u5e76\u6392\u5e8f\u3001\u5806\u6392\u5e8f\u7b49\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1) return 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n) {\nif (n <= 1) return 1;\nint count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (int i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u7ebf\u6027\u5bf9\u6570\u9636 \"\"\"\ndef linear_log_recur(n):\nif n <= 1: return 1\ncount = linear_log_recur(n // 2) + \\\n linear_log_recur(n // 2)\nfor _ in range(n):\ncount += 1\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n float64) int {\nif n <= 1 {\nreturn 1\n}\ncount := linearLogRecur(n/2) +\nlinearLogRecur(n/2)\nfor i := 0.0; i < n; i++ {\ncount++\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\nif (n <= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\nif (n <= 1) return 1;\nlet count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\nfor (let i = 0; i < n; i++) {\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{linearLogRecur}\n</code></pre> time_complexity.cs<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(float n)\n{\nif (n <= 1) return 1;\nint count = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nfor (int i = 0; i < n; i++)\n{\ncount++;\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Double) -> Int {\nif n <= 1 {\nreturn 1\n}\nvar count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\nfor _ in 0 ..< Int(n) {\ncount += 1\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: f32) i32 {\nif (n <= 1) return 1;\nvar count: i32 = linearLogRecur(n / 2) +\nlinearLogRecur(n / 2);\nvar i: f32 = 0;\nwhile (i < n) : (i += 1) {\ncount += 1;\n}\nreturn count;\n}\n</code></pre> <p></p> <p> Fig. \u7ebf\u6027\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/time_complexity/#on_1","title":"\u9636\u4e58\u9636 \\(O(n!)\\)","text":"<p>\u9636\u4e58\u9636\u5bf9\u5e94\u6570\u5b66\u4e0a\u7684\u300c\u5168\u6392\u5217\u300d\u3002\u5373\u7ed9\u5b9a \\(n\\) \u4e2a\u4e92\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u6c42\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u65b9\u6848\uff0c\u5219\u65b9\u6848\u6570\u91cf\u4e3a</p> \\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\cdots \\times 2 \\times 1 \\] <p>\u9636\u4e58\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002\u4f8b\u5982\u4ee5\u4e0b\u4ee3\u7801\uff0c\u7b2c\u4e00\u5c42\u5206\u88c2\u51fa \\(n\\) \u4e2a\uff0c\u7b2c\u4e8c\u5c42\u5206\u88c2\u51fa \\(n - 1\\) \u4e2a\uff0c\u2026\u2026 \uff0c\u76f4\u81f3\u5230\u7b2c \\(n\\) \u5c42\u65f6\u7ec8\u6b62\u5206\u88c2\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig time_complexity.java<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.cpp<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.py<pre><code>\"\"\" \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\ndef factorial_recur(n):\nif n == 0: return 1\ncount = 0\n# \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in range(n):\ncount += factorial_recur(n - 1)\nreturn count\n</code></pre> time_complexity.go<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\nif n == 0 {\nreturn 1\n}\ncount := 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor i := 0; i < n; i++ {\ncount += factorialRecur(n - 1)\n}\nreturn count\n}\n</code></pre> time_complexity.js<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\nif (n == 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.ts<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\nif (n == 0) return 1;\nlet count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (let i = 0; i < n; i++) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.c<pre><code>[class]{}-[func]{factorialRecur}\n</code></pre> time_complexity.cs<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n)\n{\nif (n == 0) return 1;\nint count = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor (int i = 0; i < n; i++)\n{\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> time_complexity.swift<pre><code>/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\nif n == 0 {\nreturn 1\n}\nvar count = 0\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nfor _ in 0 ..< n {\ncount += factorialRecur(n: n - 1)\n}\nreturn count\n}\n</code></pre> time_complexity.zig<pre><code>// \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\nif (n == 0) return 1;\nvar count: i32 = 0;\nvar i: i32 = 0;\n// \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\nwhile (i < n) : (i += 1) {\ncount += factorialRecur(n - 1);\n}\nreturn count;\n}\n</code></pre> <p></p> <p> Fig. \u9636\u4e58\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6 </p>"},{"location":"chapter_computational_complexity/time_complexity/#226","title":"2.2.6. \u00a0 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6","text":"<p>\u67d0\u4e9b\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u662f\u6052\u5b9a\u7684\uff0c\u800c\u662f\u4e0e\u8f93\u5165\u6570\u636e\u7684\u5206\u5e03\u6709\u5173\u3002\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff0c\u8f93\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u6570\u7ec4 <code>nums</code> \uff0c\u5176\u4e2d <code>nums</code> \u7531\u4ece \\(1\\) \u81f3 \\(n\\) \u7684\u6570\u5b57\u7ec4\u6210\uff0c\u4f46\u5143\u7d20\u987a\u5e8f\u662f\u968f\u673a\u6253\u4e71\u7684\uff1b\u7b97\u6cd5\u7684\u4efb\u52a1\u662f\u8fd4\u56de\u5143\u7d20 \\(1\\) \u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a</p> <ul> <li>\u5f53 <code>nums = [?, ?, ..., 1]</code>\uff0c\u5373\u5f53\u672b\u5c3e\u5143\u7d20\u662f \\(1\\) \u65f6\uff0c\u5219\u9700\u5b8c\u6574\u904d\u5386\u6570\u7ec4\uff0c\u6b64\u65f6\u8fbe\u5230 \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1b</li> <li>\u5f53 <code>nums = [1, ?, ?, ...]</code> \uff0c\u5373\u5f53\u9996\u4e2a\u6570\u5b57\u4e3a \\(1\\) \u65f6\uff0c\u65e0\u8bba\u6570\u7ec4\u591a\u957f\u90fd\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u6b64\u65f6\u8fbe\u5230 \u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(\\Omega(1)\\) \uff1b</li> </ul> <p>\u300c\u51fd\u6570\u6e10\u8fd1\u4e0a\u754c\u300d\u4f7f\u7528\u5927 \\(O\\) \u8bb0\u53f7\u8868\u793a\uff0c\u4ee3\u8868\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u3002\u4e0e\u4e4b\u5bf9\u5e94\uff0c\u300c\u51fd\u6570\u6e10\u8fd1\u4e0b\u754c\u300d\u7528 \\(\\Omega\\) \u8bb0\u53f7\uff08Omega Notation\uff09\u6765\u8868\u793a\uff0c\u4ee3\u8868\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig worst_best_time_complexity.java<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\nInteger[] nums = new Integer[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nCollections.shuffle(Arrays.asList(nums));\n// Integer[] -> int[]\nint[] res = new int[n];\nfor (int i = 0; i < n; i++) {\nres[i] = nums[i];\n}\nreturn res;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\nfor (int i = 0; i < nums.length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.cpp<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector<int> randomNumbers(int n) {\nvector<int> nums(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\nunsigned seed = chrono::system_clock::now().time_since_epoch().count();\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nshuffle(nums.begin(), nums.end(), default_random_engine(seed));\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int>& nums) {\nfor (int i = 0; i < nums.size(); i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.py<pre><code>\"\"\" \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71 \"\"\"\ndef random_numbers(n):\n# \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\nnums = [i for i in range(1, n + 1)]\n# \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrandom.shuffle(nums)\nreturn nums\n\"\"\" \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 \"\"\"\ndef find_one(nums):\nfor i in range(len(nums)):\n# \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n# \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1:\nreturn i\nreturn -1\n</code></pre> worst_best_time_complexity.go<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\nnums := make([]int, n)\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor i := 0; i < n; i++ {\nnums[i] = i + 1\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nrand.Shuffle(len(nums), func(i, j int) {\nnums[i], nums[j] = nums[j], nums[i]\n})\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\nfor i := 0; i < len(nums); i++ {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -1\n}\n</code></pre> worst_best_time_complexity.js<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i < n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\nfor (let i = 0; i < nums.length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.ts<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\nconst nums = Array(n);\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (let i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (let i = 0; i < n; i++) {\nconst r = Math.floor(Math.random() * (i + 1));\nconst temp = nums[i];\nnums[i] = nums[r];\nnums[r] = temp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\nfor (let i = 0; i < nums.length; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] === 1) {\nreturn i;\n}\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.c<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n// \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3an\uff0c\u5143\u7d20\u7c7b\u578b\u4e3aint\uff09\nint *nums = (int *)malloc(n * sizeof(int));\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++) {\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20 \nfor (int i = n - 1; i > 0; i--) {\nint j = rand() % (i + 1);\nint temp = nums[i];\nnums[i] = nums[j];\nnums[j] = temp; }\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\nfor (int i = 0; i < n; i++) {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1) return i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.cs<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n)\n{\nint[] nums = new int[n];\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nfor (int i = 0; i < n; i++)\n{\nnums[i] = i + 1;\n}\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nfor (int i = 0; i < nums.Length; i++)\n{\nvar index = new Random().Next(i, nums.Length);\nvar tmp = nums[i];\nvar ran = nums[index];\nnums[i] = ran;\nnums[index] = tmp;\n}\nreturn nums;\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums)\n{\nfor (int i = 0; i < nums.Length; i++)\n{\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif (nums[i] == 1)\nreturn i;\n}\nreturn -1;\n}\n</code></pre> worst_best_time_complexity.swift<pre><code>/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -> [Int] {\n// \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\nvar nums = Array(1 ... n)\n// \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\nnums.shuffle()\nreturn nums\n}\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\nfor i in nums.indices {\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n// \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\nif nums[i] == 1 {\nreturn i\n}\n}\nreturn -1\n}\n</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>Tip</p> <p>\u6211\u4eec\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u5f88\u5c11\u4f7f\u7528\u300c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u300d\uff0c\u56e0\u4e3a\u5f80\u5f80\u53ea\u6709\u5f88\u5c0f\u6982\u7387\u4e0b\u624d\u80fd\u8fbe\u5230\uff0c\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u8bef\u5bfc\u6027\u3002\u53cd\u4e4b\uff0c\u300c\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u6700\u4e3a\u5b9e\u7528\uff0c\u56e0\u4e3a\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u201c\u6548\u7387\u5b89\u5168\u503c\u201d\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u653e\u5fc3\u5730\u4f7f\u7528\u7b97\u6cd5\u3002</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\u5728\u201c\u7279\u6b8a\u5206\u5e03\u7684\u6570\u636e\u201d\u4e2d\uff0c\u8fd9\u4e9b\u60c5\u51b5\u7684\u51fa\u73b0\u6982\u7387\u5f80\u5f80\u5f88\u5c0f\uff0c\u56e0\u6b64\u5e76\u4e0d\u80fd\u6700\u771f\u5b9e\u5730\u53cd\u6620\u7b97\u6cd5\u8fd0\u884c\u6548\u7387\u3002\u76f8\u5bf9\u5730\uff0c\u300c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u300d\u53ef\u4ee5\u4f53\u73b0\u7b97\u6cd5\u5728\u968f\u673a\u8f93\u5165\u6570\u636e\u4e0b\u7684\u8fd0\u884c\u6548\u7387\uff0c\u7528 \\(\\Theta\\) \u8bb0\u53f7\uff08Theta Notation\uff09\u6765\u8868\u793a\u3002</p> <p>\u5bf9\u4e8e\u90e8\u5206\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u63a8\u7b97\u51fa\u968f\u673a\u6570\u636e\u5206\u5e03\u4e0b\u7684\u5e73\u5747\u60c5\u51b5\u3002\u6bd4\u5982\u4e0a\u8ff0\u793a\u4f8b\uff0c\u7531\u4e8e\u8f93\u5165\u6570\u7ec4\u662f\u88ab\u6253\u4e71\u7684\uff0c\u56e0\u6b64\u5143\u7d20 \\(1\\) \u51fa\u73b0\u5728\u4efb\u610f\u7d22\u5f15\u7684\u6982\u7387\u90fd\u662f\u76f8\u7b49\u7684\uff0c\u90a3\u4e48\u7b97\u6cd5\u7684\u5e73\u5747\u5faa\u73af\u6b21\u6570\u5219\u662f\u6570\u7ec4\u957f\u5ea6\u7684\u4e00\u534a \\(\\frac{n}{2}\\) \uff0c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(\\Theta(\\frac{n}{2}) = \\Theta(n)\\) \u3002</p> <p>\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5c24\u5176\u662f\u8f83\u4e3a\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u8ba1\u7b97\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5f88\u96be\u7b80\u4fbf\u5730\u5206\u6790\u51fa\u5728\u6570\u636e\u5206\u5e03\u4e0b\u7684\u6574\u4f53\u6570\u5b66\u671f\u671b\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e00\u822c\u4f7f\u7528\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u6765\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>\u5b9e\u9645\u4e2d\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528\u300c\u5927 \\(O\\) \u7b26\u53f7\u300d\u6765\u8868\u793a\u300c\u5e73\u5747\u590d\u6742\u5ea6\u300d\uff0c\u8fd9\u6837\u4e25\u683c\u610f\u4e49\u4e0a\u6765\u8bf4\u662f\u4e0d\u89c4\u8303\u7684\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a \\(O\\) \u7b26\u53f7\u5b9e\u5728\u662f\u592a\u6717\u6717\u4e0a\u53e3\u4e86\u3002\u5982\u679c\u5728\u672c\u4e66\u548c\u5176\u4ed6\u8d44\u6599\u4e2d\u770b\u5230\u7c7b\u4f3c \u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u7684\u8868\u8ff0\uff0c\u8bf7\u4f60\u76f4\u63a5\u7406\u89e3\u4e3a \\(\\Theta(n)\\) \u5373\u53ef\u3002</p>"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.2. \u00a0 \u6570\u636e\u7ed3\u6784\u5206\u7c7b","text":"<p>\u6570\u636e\u7ed3\u6784\u4e3b\u8981\u53ef\u6839\u636e\u300c\u903b\u8f91\u7ed3\u6784\u300d\u548c\u300c\u7269\u7406\u7ed3\u6784\u300d\u4e24\u79cd\u89d2\u5ea6\u8fdb\u884c\u5206\u7c7b\u3002</p>"},{"location":"chapter_data_structure/classification_of_data_structure/#321","title":"3.2.1. \u00a0 \u903b\u8f91\u7ed3\u6784\uff1a\u7ebf\u6027\u4e0e\u975e\u7ebf\u6027","text":"<p>\u300c\u903b\u8f91\u7ed3\u6784\u300d\u53cd\u6620\u4e86\u6570\u636e\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u3002\u6570\u7ec4\u548c\u94fe\u8868\u7684\u6570\u636e\u6309\u7167\u987a\u5e8f\u4f9d\u6b21\u6392\u5217\uff0c\u53cd\u6620\u4e86\u6570\u636e\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\uff1b\u6811\u4ece\u9876\u81f3\u5e95\u6309\u5c42\u7ea7\u6392\u5217\uff0c\u53cd\u6620\u4e86\u7956\u5148\u4e0e\u540e\u4ee3\u4e4b\u95f4\u7684\u6d3e\u751f\u5173\u7cfb\uff1b\u56fe\u7531\u7ed3\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u53cd\u6620\u4e86\u590d\u6742\u7f51\u7edc\u5173\u7cfb\u3002</p> <p>\u6211\u4eec\u4e00\u822c\u5c06\u903b\u8f91\u7ed3\u6784\u5206\u4e3a\u300c\u7ebf\u6027\u300d\u548c\u300c\u975e\u7ebf\u6027\u300d\u4e24\u79cd\u3002\u201c\u7ebf\u6027\u201d\u8fd9\u4e2a\u6982\u5ff5\u5f88\u76f4\u89c2\uff0c\u5373\u8868\u660e\u6570\u636e\u5728\u903b\u8f91\u5173\u7cfb\u4e0a\u662f\u6392\u6210\u4e00\u6761\u7ebf\u7684\uff1b\u800c\u5982\u679c\u6570\u636e\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\u662f\u975e\u7ebf\u6027\u7684\uff08\u4f8b\u5982\u662f\u7f51\u72b6\u6216\u6811\u72b6\u7684\uff09\uff0c\u90a3\u4e48\u5c31\u662f\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\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\uff1b</li> <li>\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff1a\u6811\u3001\u56fe\u3001\u5806\u3001\u54c8\u5e0c\u8868\uff1b</li> </ul> <p></p> <p> Fig. \u7ebf\u6027\u4e0e\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784 </p>"},{"location":"chapter_data_structure/classification_of_data_structure/#322","title":"3.2.2. \u00a0 \u7269\u7406\u7ed3\u6784\uff1a\u8fde\u7eed\u4e0e\u79bb\u6563","text":"<p>Note</p> <p>\u82e5\u611f\u5230\u9605\u8bfb\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u770b\u5b8c\u4e0b\u4e2a\u7ae0\u8282\u300c\u6570\u7ec4\u4e0e\u94fe\u8868\u300d\uff0c\u518d\u56de\u8fc7\u5934\u6765\u7406\u89e3\u7269\u7406\u7ed3\u6784\u7684\u542b\u4e49\u3002</p> <p>\u300c\u7269\u7406\u7ed3\u6784\u300d\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u65b9\u5f0f\u3002\u4ece\u672c\u8d28\u4e0a\u770b\uff0c\u5206\u522b\u662f \u6570\u7ec4\u7684\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8 \u548c \u94fe\u8868\u7684\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\u3002\u7269\u7406\u7ed3\u6784\u4ece\u5e95\u5c42\u4e0a\u51b3\u5b9a\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u589e\u5220\u7b49\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5728\u65f6\u95f4\u6548\u7387\u548c\u7a7a\u95f4\u6548\u7387\u65b9\u9762\u5448\u73b0\u51fa\u6b64\u6d88\u5f7c\u957f\u7684\u7279\u6027\u3002</p> <p></p> <p> Fig. \u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\u4e0e\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8 </p> <p>\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u57fa\u4e8e\u6570\u7ec4\u3001\u6216\u94fe\u8868\u3001\u6216\u4e24\u8005\u7ec4\u5408\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\u6808\u548c\u961f\u5217\uff0c\u65e2\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5b9e\u73b0\u3001\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\uff0c\u800c\u4f8b\u5982\u54c8\u5e0c\u8868\uff0c\u5176\u5b9e\u73b0\u540c\u65f6\u5305\u542b\u4e86\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\uff1b</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\uff1b</li> </ul> <p>\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u4e5f\u88ab\u79f0\u4e3a\u300c\u9759\u6001\u6570\u636e\u7ed3\u6784\u300d\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u6570\u636e\u7ed3\u6784\u5728\u5728\u88ab\u521d\u59cb\u5316\u540e\uff0c\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u76f8\u53cd\u5730\uff0c\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u88ab\u79f0\u4e3a\u300c\u52a8\u6001\u6570\u636e\u7ed3\u6784\u300d\uff0c\u8be5\u6570\u636e\u7ed3\u6784\u5728\u88ab\u521d\u59cb\u5316\u540e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u4e2d\u4fee\u6539\u5176\u957f\u5ea6\u3002</p> <p>Tip</p> <p>\u6570\u7ec4\u4e0e\u94fe\u8868\u662f\u5176\u4ed6\u6240\u6709\u6570\u636e\u7ed3\u6784\u7684\u201c\u5e95\u5c42\u79ef\u6728\u201d\uff0c\u5efa\u8bae\u8bfb\u8005\u4e00\u5b9a\u8981\u591a\u82b1\u4e9b\u65f6\u95f4\u4e86\u89e3\u3002</p>"},{"location":"chapter_data_structure/data_and_memory/","title":"3.1. \u00a0 \u6570\u636e\u4e0e\u5185\u5b58","text":""},{"location":"chapter_data_structure/data_and_memory/#311","title":"3.1.1. \u00a0 \u57fa\u672c\u6570\u636e\u7c7b\u578b","text":"<p>\u8c08\u5230\u8ba1\u7b97\u673a\u4e2d\u7684\u6570\u636e\uff0c\u6211\u4eec\u80fd\u591f\u60f3\u5230\u6587\u672c\u3001\u56fe\u7247\u3001\u89c6\u9891\u3001\u8bed\u97f3\u30013D \u6a21\u578b\u7b49\u7b49\uff0c\u8fd9\u4e9b\u6570\u636e\u867d\u7136\u7ec4\u7ec7\u5f62\u5f0f\u4e0d\u540c\uff0c\u4f46\u90fd\u662f\u7531\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u6784\u6210\u7684\u3002</p> <p>\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u662f CPU \u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8fd0\u7b97\u7684\u7c7b\u578b\uff0c\u5728\u7b97\u6cd5\u4e2d\u76f4\u63a5\u88ab\u4f7f\u7528\u3002</p> <ul> <li>\u300c\u6574\u6570\u300d\u6839\u636e\u4e0d\u540c\u7684\u957f\u5ea6\u5206\u4e3a byte, short, int, long \uff0c\u6839\u636e\u7b97\u6cd5\u9700\u6c42\u9009\u7528\uff0c\u5373\u5728\u6ee1\u8db3\u53d6\u503c\u8303\u56f4\u7684\u60c5\u51b5\u4e0b\u5c3d\u91cf\u51cf\u5c0f\u5185\u5b58\u7a7a\u95f4\u5360\u7528\uff1b</li> <li>\u300c\u6d6e\u70b9\u6570\u300d\u4ee3\u8868\u5c0f\u6570\uff0c\u6839\u636e\u957f\u5ea6\u5206\u4e3a float, double \uff0c\u540c\u6837\u6839\u636e\u7b97\u6cd5\u7684\u5b9e\u9645\u9700\u6c42\u9009\u7528\uff1b</li> <li>\u300c\u5b57\u7b26\u300d\u5728\u8ba1\u7b97\u673a\u4e2d\u662f\u4ee5\u5b57\u7b26\u96c6\u7684\u5f62\u5f0f\u4fdd\u5b58\u7684\uff0cchar \u7684\u503c\u5b9e\u9645\u4e0a\u662f\u6570\u5b57\uff0c\u4ee3\u8868\u5b57\u7b26\u96c6\u4e2d\u7684\u7f16\u53f7\uff0c\u8ba1\u7b97\u673a\u901a\u8fc7\u5b57\u7b26\u96c6\u67e5\u8868\u6765\u5b8c\u6210\u7f16\u53f7\u5230\u5b57\u7b26\u7684\u8f6c\u6362\u3002\u5360\u7528\u7a7a\u95f4\u4e0e\u5177\u4f53\u7f16\u7a0b\u8bed\u8a00\u6709\u5173\uff0c\u901a\u5e38\u4e3a 2 bytes \u6216 1 byte \uff1b</li> <li>\u300c\u5e03\u5c14\u300d\u4ee3\u8868\u903b\u8f91\u4e2d\u7684\u201c\u662f\u201d\u4e0e\u201c\u5426\u201d\uff0c\u5176\u5360\u7528\u7a7a\u95f4\u9700\u8981\u5177\u4f53\u6839\u636e\u7f16\u7a0b\u8bed\u8a00\u786e\u5b9a\uff0c\u901a\u5e38\u4e3a 1 byte \u6216 1 bit \uff1b</li> </ul> \u7c7b\u522b \u7b26\u53f7 \u5360\u7528\u7a7a\u95f4 \u53d6\u503c\u8303\u56f4 \u9ed8\u8ba4\u503c \u6574\u6570 byte 1 byte \\(-2^7\\) ~ \\(2^7 - 1\\) ( \\(-128\\) ~ \\(127\\) ) \\(0\\) short 2 bytes \\(-2^{15}\\) ~ \\(2^{15} - 1\\) \\(0\\) int 4 bytes \\(-2^{31}\\) ~ \\(2^{31} - 1\\) \\(0\\) long 8 bytes \\(-2^{63}\\) ~ \\(2^{63} - 1\\) \\(0\\) \u6d6e\u70b9\u6570 float 4 bytes \\(-3.4 \\times 10^{38}\\) ~ \\(3.4 \\times 10^{38}\\) \\(0.0\\) f double 8 bytes \\(-1.7 \\times 10^{308}\\) ~ \\(1.7 \\times 10^{308}\\) \\(0.0\\) \u5b57\u7b26 char 2 bytes / 1 byte \\(0\\) ~ \\(2^{16} - 1\\) \\(0\\) \u5e03\u5c14 bool 1 byte / 1 bit \\(\\text{true}\\) \u6216 \\(\\text{false}\\) \\(\\text{false}\\) <p>Tip</p> <p>\u4ee5\u4e0a\u8868\u683c\u4e2d\uff0c\u52a0\u7c97\u9879\u5728\u300c\u7b97\u6cd5\u9898\u300d\u4e2d\u6700\u4e3a\u5e38\u7528\u3002\u6b64\u8868\u683c\u65e0\u9700\u786c\u80cc\uff0c\u5927\u81f4\u7406\u89e3\u5373\u53ef\uff0c\u9700\u8981\u65f6\u53ef\u4ee5\u901a\u8fc7\u67e5\u8868\u6765\u56de\u5fc6\u3002</p>"},{"location":"chapter_data_structure/data_and_memory/#_1","title":"\u6574\u6570\u8868\u793a\u65b9\u5f0f","text":"<p>\u6574\u6570\u7684\u53d6\u503c\u8303\u56f4\u53d6\u51b3\u4e8e\u53d8\u91cf\u4f7f\u7528\u7684\u5185\u5b58\u957f\u5ea6\uff0c\u5373\u5b57\u8282\uff08\u6216\u6bd4\u7279\uff09\u6570\u3002\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c 1 \u5b57\u8282 (byte) = 8 \u6bd4\u7279 (bit) \uff0c 1 \u6bd4\u7279\u5373 1 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u3002\u4ee5 int \u7c7b\u578b\u4e3a\u4f8b\uff1a</p> <ol> <li>\u6574\u6570\u7c7b\u578b int \u5360\u7528 4 bytes = 32 bits \uff0c\u56e0\u6b64\u53ef\u4ee5\u8868\u793a \\(2^{32}\\) \u4e2a\u4e0d\u540c\u7684\u6570\u5b57\uff1b</li> <li>\u5c06\u6700\u9ad8\u4f4d\u770b\u4f5c\u7b26\u53f7\u4f4d\uff0c\\(0\\) \u4ee3\u8868\u6b63\u6570\uff0c\\(1\\) \u4ee3\u8868\u8d1f\u6570\uff0c\u4ece\u800c\u53ef\u4ee5\u8868\u793a \\(2^{31}\\) \u4e2a\u6b63\u6570\u548c \\(2^{31}\\) \u4e2a\u8d1f\u6570\uff1b</li> <li>\u5f53\u6240\u6709 bits \u4e3a 0 \u65f6\u4ee3\u8868\u6570\u5b57 \\(0\\) \uff0c\u4ece\u96f6\u5f00\u59cb\u589e\u5927\uff0c\u53ef\u5f97\u6700\u5927\u6b63\u6570\u4e3a \\(2^{31} - 1\\) \uff1b</li> <li>\u5269\u4f59 \\(2^{31}\\) \u4e2a\u6570\u5b57\u5168\u90e8\u7528\u6765\u8868\u793a\u8d1f\u6570\uff0c\u56e0\u6b64\u6700\u5c0f\u8d1f\u6570\u4e3a \\(-2^{31}\\) \uff1b\u5177\u4f53\u7ec6\u8282\u6d89\u53ca\u5230\u5230\u201c\u6e90\u7801\u3001\u53cd\u7801\u3001\u8865\u7801\u201d\u77e5\u8bc6\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u67e5\u9605\u5b66\u4e60\uff1b </li> </ol> <p>\u5176\u5b83\u6574\u6570\u7c7b\u578b byte, short, long \u53d6\u503c\u8303\u56f4\u7684\u8ba1\u7b97\u65b9\u6cd5\u4e0e int \u7c7b\u4f3c\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002</p>"},{"location":"chapter_data_structure/data_and_memory/#_2","title":"\u6d6e\u70b9\u6570\u8868\u793a\u65b9\u5f0f *","text":"<p>Note</p> <p>\u5728\u672c\u4e66\u4e2d\uff0c\u6807\u9898\u540e\u7684 <code>*</code> \u7b26\u53f7\u4ee3\u8868\u9009\u8bfb\u7ae0\u8282\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u7406\u89e3\u56f0\u96be\uff0c\u5efa\u8bae\u5148\u8df3\u8fc7\uff0c\u7b49\u5b66\u5b8c\u5fc5\u8bfb\u7ae0\u8282\u540e\u7eed\u518d\u5355\u72ec\u653b\u514b\u3002 </p> <p>\u7ec6\u5fc3\u7684\u4f60\u53ef\u80fd\u4f1a\u7591\u60d1\uff1a int \u548c float \u957f\u5ea6\u76f8\u540c\uff0c\u90fd\u662f 4 bytes \uff0c\u4f46\u4e3a\u4ec0\u4e48 float \u7684\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \uff1f\u6309\u8bf4 float \u9700\u8981\u8868\u793a\u5c0f\u6570\uff0c\u53d6\u503c\u8303\u56f4\u5e94\u8be5\u53d8\u5c0f\u624d\u5bf9\u3002</p> <p>\u5176\u5b9e\uff0c\u8fd9\u662f\u56e0\u4e3a\u6d6e\u70b9\u6570 float \u91c7\u7528\u4e86\u4e0d\u540c\u7684\u8868\u793a\u65b9\u5f0f\u3002IEEE 754 \u6807\u51c6\u89c4\u5b9a\uff0c32-bit \u957f\u5ea6\u7684 float \u7531\u4ee5\u4e0b\u90e8\u5206\u6784\u6210\uff1a</p> <ul> <li>\u7b26\u53f7\u4f4d \\(\\mathrm{S}\\) \uff1a\u5360 1 bit \uff1b</li> <li>\u6307\u6570\u4f4d \\(\\mathrm{E}\\) \uff1a\u5360 8 bits \uff1b</li> <li>\u5206\u6570\u4f4d \\(\\mathrm{N}\\) \uff1a\u5360 24 bits \uff0c\u5176\u4e2d 23 \u4f4d\u663e\u5f0f\u5b58\u50a8\uff1b</li> </ul> <p>\u8bbe 32-bit \u4e8c\u8fdb\u5236\u6570\u7684\u7b2c \\(i\\) \u4f4d\u4e3a \\(b_i\\) \uff0c\u5219 float \u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u5b9a\u4e49\u4e3a</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\u4e3a</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\u4e3a</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> Fig. IEEE 754 \u6807\u51c6\u4e0b\u7684 float \u8868\u793a\u65b9\u5f0f </p> <p>\u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\\(\\mathrm{S} = 0\\) \uff0c \\(\\mathrm{E} = 124\\) \uff0c\\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) \uff0c\u6613\u5f97</p> \\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\] <p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u56de\u7b54\u5f00\u59cb\u7684\u95ee\u9898\uff1afloat \u7684\u8868\u793a\u65b9\u5f0f\u5305\u542b\u6307\u6570\u4f4d\uff0c\u5bfc\u81f4\u5176\u53d6\u503c\u8303\u56f4\u8fdc\u5927\u4e8e int \u3002\u6839\u636e\u4ee5\u4e0a\u8ba1\u7b97\uff0c float \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>\u6d6e\u70b9\u6570 float \u867d\u7136\u62d3\u5c55\u4e86\u53d6\u503c\u8303\u56f4\uff0c\u4f46\u526f\u4f5c\u7528\u662f\u727a\u7272\u4e86\u7cbe\u5ea6\u3002\u6574\u6570\u7c7b\u578b int \u5c06\u5168\u90e8 32 \u4f4d\u7528\u4e8e\u8868\u793a\u6570\u5b57\uff0c\u6570\u5b57\u662f\u5747\u5300\u5206\u5e03\u7684\uff1b\u800c\u7531\u4e8e\u6307\u6570\u4f4d\u7684\u5b58\u5728\uff0c\u6d6e\u70b9\u6570 float \u7684\u6570\u503c\u8d8a\u5927\uff0c\u76f8\u90bb\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7684\u5dee\u503c\u5c31\u4f1a\u8d8b\u5411\u8d8a\u5927\u3002</p> <p>\u8fdb\u4e00\u6b65\u5730\uff0c\u6307\u6570\u4f4d \\(E = 0\\) \u548c \\(E = 255\\) \u5177\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u7528\u4e8e\u8868\u793a\u96f6\u3001\u65e0\u7a77\u5927\u3001\\(\\mathrm{NaN}\\) \u7b49\u3002</p> \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\uff08subnormal number\uff09 \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u6b63\u89c4\u6570 \u6b63\u89c4\u6570 \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\) <p>\u7279\u522b\u5730\uff0c\u6b21\u6b63\u89c4\u6570\u663e\u8457\u63d0\u5347\u4e86\u5c0f\u6570\u7cbe\u5ea6\uff1a</p> <ul> <li>\u6700\u5c0f\u6b63\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\approx 1.18 \\times 10^{-38}\\) \uff1b</li> <li>\u6700\u5c0f\u6b63\u6b21\u6b63\u89c4\u6570\u4e3a \\(2^{-126} \\times 2^{-23} \\approx 1.4 \\times 10^{-45}\\) \uff1b</li> </ul> <p>\u53cc\u7cbe\u5ea6 double \u4e5f\u91c7\u7528\u7c7b\u4f3c float \u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002</p>"},{"location":"chapter_data_structure/data_and_memory/#_3","title":"\u57fa\u672c\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7ed3\u6784\u7684\u5173\u7cfb","text":"<p>\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\uff0c\u5b83\u7684\u4e3b\u8bed\u662f\u201c\u7ed3\u6784\u201d\uff0c\u800c\u4e0d\u662f\u201c\u6570\u636e\u201d\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u8868\u793a\u201c\u4e00\u6392\u6570\u5b57\u201d\uff0c\u81ea\u7136\u60f3\u5230\u4f7f\u7528\u300c\u6570\u7ec4\u300d\u6570\u636e\u7ed3\u6784\u3002\u6570\u7ec4\u7684\u5b58\u50a8\u65b9\u5f0f\u53ef\u4ee5\u8868\u793a\u6570\u5b57\u7684\u76f8\u90bb\u5173\u7cfb\u3001\u987a\u5e8f\u5173\u7cfb\uff0c\u4f46\u81f3\u4e8e\u5176\u4e2d\u5b58\u50a8\u7684\u662f\u6574\u6570 int \uff0c\u8fd8\u662f\u5c0f\u6570 float \uff0c\u6216\u662f\u5b57\u7b26 char \uff0c\u5219\u4e0e\u6240\u8c13\u7684\u6570\u636e\u7684\u7ed3\u6784\u65e0\u5173\u4e86\u3002</p> <p>\u6362\u8a00\u4e4b\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\u6570\u636e\u7684\u201c\u7ec4\u7ec7\u65b9\u5f0f\u201d\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>/* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] booleans = new boolean[5];\n</code></pre> <pre><code>/* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool booleans[5];\n</code></pre> <pre><code>\"\"\" Python \u7684 list \u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61 \"\"\"\nlist = [0, 0.0, 'a', False]\n</code></pre> <pre><code>// \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar booleans = [5]bool{}\n</code></pre> <pre><code>/* JavaScript \u7684\u6570\u7ec4\u53ef\u4ee5\u81ea\u7531\u5b58\u50a8\u5404\u79cd\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u5bf9\u8c61 */\nconst array = [0, 0.0, 'a', false];\n</code></pre> <pre><code>/* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst booleans: boolean[] = [];\n</code></pre> <pre><code>/* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool booleans[10];\n</code></pre> <pre><code>/* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] booleans = new bool[5];\n</code></pre> <pre><code>/* \u4f7f\u7528\u591a\u79cd\u300c\u57fa\u672c\u6570\u636e\u7c7b\u578b\u300d\u6765\u521d\u59cb\u5316\u300c\u6570\u7ec4\u300d */\nlet numbers = Array(repeating: Int(), count: 5)\nlet decimals = Array(repeating: Double(), count: 5)\nlet characters = Array(repeating: Character(\"a\"), count: 5)\nlet booleans = Array(repeating: Bool(), count: 5)\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_data_structure/data_and_memory/#312","title":"3.1.2. \u00a0 \u8ba1\u7b97\u673a\u5185\u5b58","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\u300c\u786c\u76d8\u300d\u4e3b\u8981\u7528\u4e8e\u957f\u671f\u5b58\u50a8\u6570\u636e\uff0c\u5bb9\u91cf\u8f83\u5927\uff08\u901a\u5e38\u53ef\u8fbe\u5230 TB \u7ea7\u522b\uff09\u3001\u901f\u5ea6\u8f83\u6162\u3002\u300c\u5185\u5b58\u300d\u7528\u4e8e\u8fd0\u884c\u7a0b\u5e8f\u65f6\u6682\u5b58\u6570\u636e\uff0c\u901f\u5ea6\u8f83\u5feb\uff0c\u4f46\u5bb9\u91cf\u8f83\u5c0f\uff08\u901a\u5e38\u4e3a GB \u7ea7\u522b\uff09\u3002</p> <p>\u7b97\u6cd5\u8fd0\u884c\u4e2d\uff0c\u76f8\u5173\u6570\u636e\u90fd\u88ab\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u3002\u4e0b\u56fe\u5c55\u793a\u4e86\u4e00\u4e2a\u8ba1\u7b97\u673a\u5185\u5b58\u6761\uff0c\u5176\u4e2d\u6bcf\u4e2a\u9ed1\u8272\u65b9\u5757\u90fd\u5305\u542b\u4e00\u5757\u5185\u5b58\u7a7a\u95f4\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u5185\u5b58\u60f3\u8c61\u6210\u4e00\u4e2a\u5de8\u5927\u7684 Excel \u8868\u683c\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u90fd\u53ef\u4ee5\u5b58\u50a8 1 byte \u7684\u6570\u636e\uff0c\u5728\u7b97\u6cd5\u8fd0\u884c\u65f6\uff0c\u6240\u6709\u6570\u636e\u90fd\u88ab\u5b58\u50a8\u5728\u8fd9\u4e9b\u5355\u5143\u683c\u4e2d\u3002</p> <p>\u7cfb\u7edf\u901a\u8fc7\u300c\u5185\u5b58\u5730\u5740 Memory Location\u300d\u6765\u8bbf\u95ee\u76ee\u6807\u5185\u5b58\u4f4d\u7f6e\u7684\u6570\u636e\u3002\u8ba1\u7b97\u673a\u6839\u636e\u7279\u5b9a\u89c4\u5219\u7ed9\u8868\u683c\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u7f16\u53f7\uff0c\u4fdd\u8bc1\u6bcf\u5757\u5185\u5b58\u7a7a\u95f4\u90fd\u6709\u72ec\u7acb\u7684\u5185\u5b58\u5730\u5740\u3002\u81ea\u6b64\uff0c\u7a0b\u5e8f\u4fbf\u901a\u8fc7\u8fd9\u4e9b\u5730\u5740\uff0c\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u3002</p> <p></p> <p> Fig. \u5185\u5b58\u6761\u3001\u5185\u5b58\u7a7a\u95f4\u3001\u5185\u5b58\u5730\u5740 </p> <p>\u5185\u5b58\u8d44\u6e90\u662f\u8bbe\u8ba1\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u91cd\u8981\u8003\u8651\u56e0\u7d20\u3002\u5185\u5b58\u662f\u6240\u6709\u7a0b\u5e8f\u7684\u516c\u5171\u8d44\u6e90\uff0c\u5f53\u5185\u5b58\u88ab\u67d0\u7a0b\u5e8f\u5360\u7528\u65f6\uff0c\u4e0d\u80fd\u88ab\u5176\u5b83\u7a0b\u5e8f\u540c\u65f6\u4f7f\u7528\u3002\u6211\u4eec\u9700\u8981\u6839\u636e\u5269\u4f59\u5185\u5b58\u8d44\u6e90\u7684\u60c5\u51b5\u6765\u8bbe\u8ba1\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u82e5\u5269\u4f59\u5185\u5b58\u7a7a\u95f4\u6709\u9650\uff0c\u5219\u8981\u6c42\u7b97\u6cd5\u5360\u7528\u7684\u5cf0\u503c\u5185\u5b58\u4e0d\u80fd\u8d85\u8fc7\u7cfb\u7edf\u5269\u4f59\u5185\u5b58\uff1b\u82e5\u8fd0\u884c\u7684\u7a0b\u5e8f\u5f88\u591a\u3001\u7f3a\u5c11\u5927\u5757\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5219\u8981\u6c42\u9009\u53d6\u7684\u6570\u636e\u7ed3\u6784\u5fc5\u987b\u80fd\u591f\u5b58\u50a8\u5728\u79bb\u6563\u7684\u5185\u5b58\u7a7a\u95f4\u5185\u3002</p>"},{"location":"chapter_data_structure/summary/","title":"3.3. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u6574\u6570 byte, short, int, long \u3001\u6d6e\u70b9\u6570 float, double \u3001\u5b57\u7b26 char \u3001\u5e03\u5c14 boolean \u662f\u8ba1\u7b97\u673a\u4e2d\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u5360\u7528\u7a7a\u95f4\u7684\u5927\u5c0f\u51b3\u5b9a\u4e86\u5b83\u4eec\u7684\u53d6\u503c\u8303\u56f4\u3002</li> <li>\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u6570\u636e\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u7684\u5185\u5b58\u4e2d\u3002\u5185\u5b58\u4e2d\u6bcf\u5757\u7a7a\u95f4\u90fd\u6709\u72ec\u7acb\u7684\u5185\u5b58\u5730\u5740\uff0c\u7a0b\u5e8f\u662f\u901a\u8fc7\u5185\u5b58\u5730\u5740\u6765\u8bbf\u95ee\u6570\u636e\u7684\u3002</li> <li>\u6570\u636e\u7ed3\u6784\u4e3b\u8981\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\u53cd\u6620\u4e86\u6570\u636e\u4e2d\u5143\u7d20\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u7269\u7406\u7ed3\u6784\u53cd\u6620\u4e86\u6570\u636e\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u5f62\u5f0f\u3002</li> <li>\u5e38\u89c1\u7684\u903b\u8f91\u7ed3\u6784\u6709\u7ebf\u6027\u3001\u6811\u72b6\u3001\u7f51\u72b6\u7b49\u3002\u6211\u4eec\u4e00\u822c\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\u6839\u636e\u5b9e\u73b0\u65b9\u5f0f\u7684\u4e0d\u540c\uff0c\u54c8\u5e0c\u8868\u53ef\u80fd\u662f\u7ebf\u6027\u6216\u975e\u7ebf\u6027\u3002</li> <li>\u7269\u7406\u7ed3\u6784\u4e3b\u8981\u6709\u4e24\u79cd\uff0c\u5206\u522b\u662f\u8fde\u7eed\u7a7a\u95f4\u5b58\u50a8\uff08\u6570\u7ec4\uff09\u548c\u79bb\u6563\u7a7a\u95f4\u5b58\u50a8\uff08\u94fe\u8868\uff09\uff0c\u6240\u6709\u7684\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7531\u6570\u7ec4\u3001\u6216\u94fe\u8868\u3001\u6216\u4e24\u8005\u7ec4\u5408\u5b9e\u73b0\u7684\u3002</li> </ul>"},{"location":"chapter_graph/graph/","title":"9.1. \u00a0 \u56fe\uff08Graph\uff09","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\u5c06\u56fe \\(G\\) \u62bd\u8c61\u5730\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9 \\(V\\) \u548c\u4e00\u7ec4\u8fb9 \\(E\\) \u7684\u96c6\u5408\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u8868\u793a\u4e00\u4e2a\u5305\u542b 5 \u4e2a\u9876\u70b9\u548c 7 \u6761\u8fb9\u7684\u56fe</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></p> <p> Fig. \u94fe\u8868\u3001\u6811\u3001\u56fe\u4e4b\u95f4\u7684\u5173\u7cfb </p> <p>\u90a3\u4e48\uff0c\u56fe\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u5173\u7cfb\u662f\u4ec0\u4e48\uff1f\u5982\u679c\u6211\u4eec\u628a\u300c\u9876\u70b9\u300d\u770b\u4f5c\u7ed3\u70b9\uff0c\u628a\u300c\u8fb9\u300d\u770b\u4f5c\u8fde\u63a5\u5404\u4e2a\u7ed3\u70b9\u7684\u6307\u9488\uff0c\u5219\u53ef\u5c06\u300c\u56fe\u300d\u770b\u6210\u4e00\u79cd\u4ece\u300c\u94fe\u8868\u300d\u62d3\u5c55\u800c\u6765\u7684\u6570\u636e\u7ed3\u6784\u3002\u76f8\u6bd4\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\u4e5f\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002</p>"},{"location":"chapter_graph/graph/#911","title":"9.1.1. \u00a0 \u56fe\u5e38\u89c1\u7c7b\u578b","text":"<p>\u6839\u636e\u8fb9\u662f\u5426\u6709\u65b9\u5411\uff0c\u5206\u4e3a\u300c\u65e0\u5411\u56fe Undirected Graph\u300d\u548c\u300c\u6709\u5411\u56fe Directed Graph\u300d\u3002</p> <ul> <li>\u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u8fb9\u8868\u793a\u4e24\u9876\u70b9\u4e4b\u95f4\u201c\u53cc\u5411\u201d\u7684\u8fde\u63a5\u5173\u7cfb\uff0c\u4f8b\u5982\u5fae\u4fe1\u6216 QQ \u4e2d\u7684\u201c\u597d\u53cb\u5173\u7cfb\u201d\uff1b</li> <li>\u5728\u6709\u5411\u56fe\u4e2d\uff0c\u8fb9\u662f\u6709\u65b9\u5411\u7684\uff0c\u5373 \\(A \\rightarrow B\\) \u548c \\(A \\leftarrow B\\) \u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u4f8b\u5982\u5fae\u535a\u6216\u6296\u97f3\u4e0a\u7684\u201c\u5173\u6ce8\u201d\u4e0e\u201c\u88ab\u5173\u6ce8\u201d\u5173\u7cfb\uff1b</li> </ul> <p></p> <p> Fig. \u6709\u5411\u56fe\u4e0e\u65e0\u5411\u56fe </p> <p>\u6839\u636e\u6240\u6709\u9876\u70b9\u662f\u5426\u8fde\u901a\uff0c\u5206\u4e3a\u300c\u8fde\u901a\u56fe Connected Graph\u300d\u548c\u300c\u975e\u8fde\u901a\u56fe Disconnected Graph\u300d\u3002</p> <ul> <li>\u5bf9\u4e8e\u8fde\u901a\u56fe\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u53ef\u4ee5\u5230\u8fbe\u5176\u4f59\u4efb\u610f\u9876\u70b9\uff1b</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\uff1b</li> </ul> <p></p> <p> Fig. \u8fde\u901a\u56fe\u4e0e\u975e\u8fde\u901a\u56fe </p> <p>\u6211\u4eec\u53ef\u4ee5\u7ed9\u8fb9\u6dfb\u52a0\u201c\u6743\u91cd\u201d\u53d8\u91cf\uff0c\u5f97\u5230\u300c\u6709\u6743\u56fe Weighted Graph\u300d\u3002\u4f8b\u5982\uff0c\u5728\u738b\u8005\u8363\u8000\u7b49\u6e38\u620f\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\u4f7f\u7528\u6709\u6743\u56fe\u6765\u8868\u793a\u3002</p> <p></p> <p> Fig. \u6709\u6743\u56fe\u4e0e\u65e0\u6743\u56fe </p>"},{"location":"chapter_graph/graph/#912","title":"9.1.2. \u00a0 \u56fe\u5e38\u7528\u672f\u8bed","text":"<ul> <li>\u300c\u90bb\u63a5 Adjacency\u300d\uff1a\u5f53\u4e24\u9876\u70b9\u4e4b\u95f4\u6709\u8fb9\u76f8\u8fde\u65f6\uff0c\u79f0\u6b64\u4e24\u9876\u70b9\u201c\u90bb\u63a5\u201d\u3002\u4f8b\u5982\uff0c\u4e0a\u56fe\u4e2d\u9876\u70b9 1 \u7684\u90bb\u63a5\u9876\u70b9\u4e3a\u9876\u70b9 2, 3, 5 \u3002</li> <li>\u300c\u8def\u5f84 Path\u300d\uff1a\u4ece\u9876\u70b9 A \u5230\u9876\u70b9 B \u8d70\u8fc7\u7684\u8fb9\u6784\u6210\u7684\u5e8f\u5217\uff0c\u88ab\u79f0\u4e3a\u4ece A \u5230 B \u7684\u201c\u8def\u5f84\u201d\u3002\u4f8b\u5982\uff0c\u4e0a\u56fe\u4e2d 1, 5, 2, 4 \u662f\u9876\u70b9 1 \u5230\u9876\u70b9 4 \u7684\u4e00\u4e2a\u8def\u5f84\u3002</li> <li>\u300c\u5ea6 Degree\u300d\u8868\u793a\u4e00\u4e2a\u9876\u70b9\u5177\u6709\u591a\u5c11\u6761\u8fb9\u3002\u5bf9\u4e8e\u6709\u5411\u56fe\uff0c\u300c\u5165\u5ea6 In-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u6307\u5411\u8be5\u9876\u70b9\uff0c\u300c\u51fa\u5ea6 Out-Degree\u300d\u8868\u793a\u6709\u591a\u5c11\u6761\u8fb9\u4ece\u8be5\u9876\u70b9\u6307\u51fa\u3002</li> </ul>"},{"location":"chapter_graph/graph/#913","title":"9.1.3. \u00a0 \u56fe\u7684\u8868\u793a","text":"<p>\u56fe\u7684\u5e38\u7528\u8868\u793a\u65b9\u6cd5\u6709\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\u3002\u4ee5\u4e0b\u4f7f\u7528\u300c\u65e0\u5411\u56fe\u300d\u6765\u4e3e\u4f8b\u3002</p>"},{"location":"chapter_graph/graph/#_1","title":"\u90bb\u63a5\u77e9\u9635","text":"<p>\u8bbe\u56fe\u7684\u9876\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u300c\u90bb\u63a5\u77e9\u9635 Adjacency Matrix\u300d\u4f7f\u7528\u4e00\u4e2a \\(n \\times n\\) \u5927\u5c0f\u7684\u77e9\u9635\u6765\u8868\u793a\u56fe\uff0c\u6bcf\u4e00\u884c\uff08\u5217\uff09\u4ee3\u8868\u4e00\u4e2a\u9876\u70b9\uff0c\u77e9\u9635\u5143\u7d20\u4ee3\u8868\u8fb9\uff0c\u4f7f\u7528 \\(1\\) \u6216 \\(0\\) \u6765\u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u6709\u8fb9\u6216\u65e0\u8fb9\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u8bb0\u90bb\u63a5\u77e9\u9635\u4e3a \\(M\\) \u3001\u9876\u70b9\u5217\u8868\u4e3a \\(V\\) \uff0c\u5219\u77e9\u9635\u5143\u7d20 \\(M[i][j] = 1\\) \u4ee3\u8868\u7740\u9876\u70b9 \\(V[i]\\) \u5230\u9876\u70b9 \\(V[j]\\) \u4e4b\u95f4\u6709\u8fb9\uff0c\u76f8\u53cd\u5730 \\(M[i][j] = 0\\) \u4ee3\u8868\u4e24\u9876\u70b9\u4e4b\u95f4\u65e0\u8fb9\u3002</p> <p></p> <p> Fig. \u56fe\u7684\u90bb\u63a5\u77e9\u9635\u8868\u793a </p> <p>\u90bb\u63a5\u77e9\u9635\u5177\u6709\u4ee5\u4e0b\u6027\u8d28\uff1a</p> <ul> <li>\u9876\u70b9\u4e0d\u80fd\u4e0e\u81ea\u8eab\u76f8\u8fde\uff0c\u56e0\u800c\u90bb\u63a5\u77e9\u9635\u4e3b\u5bf9\u89d2\u7ebf\u5143\u7d20\u6ca1\u6709\u610f\u4e49\u3002</li> <li>\u300c\u65e0\u5411\u56fe\u300d\u4e24\u4e2a\u65b9\u5411\u7684\u8fb9\u7b49\u4ef7\uff0c\u6b64\u65f6\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\u3002</li> <li>\u5c06\u90bb\u63a5\u77e9\u9635\u7684\u5143\u7d20\u4ece \\(1\\) , \\(0\\) \u66ff\u6362\u4e3a\u6743\u91cd\uff0c\u5219\u80fd\u591f\u8868\u793a\u300c\u6709\u6743\u56fe\u300d\u3002</li> </ul> <p>\u4f7f\u7528\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u8bbf\u95ee\u77e9\u9635\u5143\u7d20\u6765\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\u5927\u3002</p>"},{"location":"chapter_graph/graph/#_2","title":"\u90bb\u63a5\u8868","text":"<p>\u300c\u90bb\u63a5\u8868 Adjacency List\u300d\u4f7f\u7528 \\(n\\) \u4e2a\u94fe\u8868\u6765\u8868\u793a\u56fe\uff0c\u94fe\u8868\u7ed3\u70b9\u8868\u793a\u9876\u70b9\u3002\u7b2c \\(i\\) \u6761\u94fe\u8868\u5bf9\u5e94\u9876\u70b9 \\(i\\) \uff0c\u5176\u4e2d\u5b58\u50a8\u4e86\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff08\u5373\u4e0e\u8be5\u9876\u70b9\u76f8\u8fde\u7684\u9876\u70b9\uff09\u3002</p> <p></p> <p> Fig. \u56fe\u7684\u90bb\u63a5\u8868\u8868\u793a </p> <p>\u90bb\u63a5\u8868\u4ec5\u5b58\u50a8\u5b58\u5728\u7684\u8fb9\uff0c\u800c\u8fb9\u7684\u603b\u6570\u5f80\u5f80\u8fdc\u5c0f\u4e8e \\(n^2\\) \uff0c\u56e0\u6b64\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\u3002\u4f46\u662f\uff0c\u56e0\u4e3a\u5728\u90bb\u63a5\u8868\u4e2d\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u6240\u4ee5\u5176\u65f6\u95f4\u6548\u7387\u4e0d\u5982\u90bb\u63a5\u77e9\u9635\u3002</p> <p>\u89c2\u5bdf\u4e0a\u56fe\u53d1\u73b0\uff0c\u90bb\u63a5\u8868\u7ed3\u6784\u4e0e\u54c8\u5e0c\u8868\u300c\u94fe\u5730\u5740\u6cd5\u300d\u975e\u5e38\u76f8\u4f3c\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528\u7c7b\u4f3c\u65b9\u6cd5\u6765\u4f18\u5316\u6548\u7387\u3002\u6bd4\u5982\uff0c\u5f53\u94fe\u8868\u8f83\u957f\u65f6\uff0c\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c\u4ece\u800c\u5c06\u65f6\u95f4\u6548\u7387\u4ece \\(O(n)\\) \u4f18\u5316\u81f3 \\(O(\\log n)\\) \uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e2d\u5e8f\u904d\u5386\u83b7\u53d6\u6709\u5e8f\u5e8f\u5217\uff1b\u8fd8\u53ef\u4ee5\u5c06\u94fe\u8868\u8f6c\u5316\u4e3a\u54c8\u5e0c\u8868\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u81f3 \\(O(1)\\) \u3002</p>"},{"location":"chapter_graph/graph/#914","title":"9.1.4. \u00a0 \u56fe\u5e38\u89c1\u5e94\u7528","text":"<p>\u73b0\u5b9e\u4e2d\u7684\u8bb8\u591a\u7cfb\u7edf\u90fd\u53ef\u4ee5\u4f7f\u7528\u56fe\u6765\u5efa\u6a21\uff0c\u5bf9\u5e94\u7684\u5f85\u6c42\u89e3\u95ee\u9898\u4e5f\u53ef\u4ee5\u88ab\u7ea6\u5316\u4e3a\u56fe\u8ba1\u7b97\u95ee\u9898\u3002</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\u5206\u4e3a\u5bf9\u300c\u8fb9\u300d\u7684\u64cd\u4f5c\u548c\u5bf9\u300c\u9876\u70b9\u300d\u7684\u64cd\u4f5c\uff0c\u5728\u300c\u90bb\u63a5\u77e9\u9635\u300d\u548c\u300c\u90bb\u63a5\u8868\u300d\u8fd9\u4e24\u79cd\u8868\u793a\u4e0b\u7684\u5b9e\u73b0\u65b9\u5f0f\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>\u8bbe\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6709\uff1a</p> <ul> <li>\u6dfb\u52a0\u6216\u5220\u9664\u8fb9\uff1a\u76f4\u63a5\u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u4fee\u6539\u6307\u5b9a\u8fb9\u7684\u5bf9\u5e94\u5143\u7d20\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>\u4ee5\u4e0b\u662f\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u8868\u793a\u56fe\u7684\u5b9e\u73b0\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig graph_adjacency_matrix.java<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nList<Integer> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nList<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjMat(int[] vertices, int[][] edges) {\nthis.vertices = new ArrayList<>();\nthis.adjMat = new ArrayList<>();\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (int[] e : edges) {\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<Integer> newRow = new ArrayList<>(n);\nfor (int j = 0; j < n; j++) {\nnewRow.add(0);\n}\nadjMat.add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (List<Integer> row : adjMat) {\nrow.add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index) {\nif (index >= size())\nthrow new IndexOutOfBoundsException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (List<Integer> row : adjMat) {\nrow.remove(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfBoundsException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat.get(i).set(j, 1);\nadjMat.get(j).set(i, 1);\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfBoundsException();\nadjMat.get(i).set(j, 0);\nadjMat.get(j).set(i, 0);\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print() {\nSystem.out.print(\"\u9876\u70b9\u5217\u8868 = \");\nSystem.out.println(vertices);\nSystem.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.printMatrix(adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.cpp<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvector<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nvector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjMat(const vector<int>& vertices, const vector<vector<int>>& edges) {\n// \u6dfb\u52a0\u9876\u70b9\nfor (int val : vertices) {\naddVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (const vector<int>& edge : edges) {\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() const {\nreturn vertices.size();\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(int val) {\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.push_back(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nadjMat.emplace_back(n, 0);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (vector<int>& row : adjMat) {\nrow.push_back(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(int index) {\nif (index >= size()) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.erase(vertices.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.erase(adjMat.begin() + index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (vector<int>& row : adjMat) {\nrow.erase(row.begin() + index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(int i, int j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\nthrow out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n}\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid print() {\ncout << \"\u9876\u70b9\u5217\u8868 = \";\nPrintUtil::printVector(vertices);\ncout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\nPrintUtil::printVectorMatrix(adjMat);\n}\n};\n</code></pre> graph_adjacency_matrix.py<pre><code>\"\"\" \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b \"\"\"\nclass GraphAdjMat:\n# \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nvertices = []\n# \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadj_mat = []\n\"\"\" \u6784\u9020\u65b9\u6cd5 \"\"\"\ndef __init__(self, vertices, edges):\nself.vertices = []\nself.adj_mat = []\n# \u6dfb\u52a0\u9876\u70b9\nfor val in vertices:\nself.add_vertex(val)\n# \u6dfb\u52a0\u8fb9\n# \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor e in edges:\nself.add_edge(e[0], e[1])\n\"\"\" \u83b7\u53d6\u9876\u70b9\u6570\u91cf \"\"\"\ndef size(self):\nreturn len(self.vertices)\n\"\"\" \u6dfb\u52a0\u9876\u70b9 \"\"\"\ndef add_vertex(self, val):\nn = self.size()\n# \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nself.vertices.append(val)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnew_row = [0]*n\nself.adj_mat.append(new_row)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor row in self.adj_mat:\nrow.append(0)\n\"\"\" \u5220\u9664\u9876\u70b9 \"\"\"\ndef remove_vertex(self, index):\nif index >= self.size():\nraise IndexError()\n# \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nself.vertices.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nself.adj_mat.pop(index)\n# \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor row in self.adj_mat:\nrow.pop(index)\n\"\"\" \u6dfb\u52a0\u8fb9 \"\"\"\n# \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\ndef add_edge(self, i, j):\n# \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\nraise IndexError()\n# \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nself.adj_mat[i][j] = 1\nself.adj_mat[j][i] = 1\n\"\"\" \u5220\u9664\u8fb9 \"\"\"\n# \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\ndef remove_edge(self, i, j):\n# \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\nraise IndexError()\nself.adj_mat[i][j] = 0\nself.adj_mat[j][i] = 0\n\"\"\" \u6253\u5370\u90bb\u63a5\u77e9\u9635 \"\"\"\ndef print(self):\nprint(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\nprint(\"\u90bb\u63a5\u77e9\u9635 =\")\nprint_matrix(self.adj_mat)\n</code></pre> graph_adjacency_matrix.go<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n// \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nvertices []int\n// \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat [][]int\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n// \u6dfb\u52a0\u9876\u70b9\nn := len(vertices)\nadjMat := make([][]int, n)\nfor i := range adjMat {\nadjMat[i] = make([]int, n)\n}\n// \u521d\u59cb\u5316\u56fe\ng := &graphAdjMat{\nvertices: vertices,\nadjMat: adjMat,\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor i := range edges {\ng.addEdge(edges[i][0], edges[i][1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\nreturn len(g.vertices)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\nn := g.size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\ng.vertices = append(g.vertices, val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nnewRow := make([]int, n)\ng.adjMat = append(g.adjMat, newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i], 0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\nif index >= g.size() {\nreturn\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\ng.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\ng.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i := range g.adjMat {\ng.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\ng.adjMat[i][j] = 1\ng.adjMat[j][i] = 1\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\nfmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n}\ng.adjMat[i][j] = 0\ng.adjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\nfmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\nfmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\nfor i := range g.adjMat {\nfmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n}\n}\n</code></pre> graph_adjacency_matrix.js<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices, edges) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.addVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val) {\nconst n = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow = [];\nfor (let j = 0; j < n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index) {\nif (index >= this.size()) {\nthrow new RangeError(\"Index Out Of Bounds Exception\");\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError(\"Index Out Of Bounds Exception\");\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i, j) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError(\"Index Out Of Bounds Exception\");\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint() {\nconsole.log(\"\u9876\u70b9\u5217\u8868 = \", this.vertices);\nconsole.log(\"\u90bb\u63a5\u77e9\u9635 =\", this.adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.ts<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nvertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nadjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\nconstructor(vertices: number[], edges: number[][]) {\nthis.vertices = [];\nthis.adjMat = [];\n// \u6dfb\u52a0\u9876\u70b9\nfor (const val of vertices) {\nthis.addVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor (const e of edges) {\nthis.addEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.vertices.length;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(val: number): void {\nconst n: number = this.size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nthis.vertices.push(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nconst newRow: number[] = [];\nfor (let j: number = 0; j < n; j++) {\nnewRow.push(0);\n}\nthis.adjMat.push(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor (const row of this.adjMat) {\nrow.push(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(index: number): void {\nif (index >= this.size()) {\nthrow new RangeError(\"Index Out Of Bounds Exception\");\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nthis.vertices.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nthis.adjMat.splice(index, 1);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor (const row of this.adjMat) {\nrow.splice(index, 1);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\naddEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError(\"Index Out Of Bounds Exception\");\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nthis.adjMat[i][j] = 1;\nthis.adjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nremoveEdge(i: number, j: number): void {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\nthrow new RangeError(\"Index Out Of Bounds Exception\");\n}\nthis.adjMat[i][j] = 0;\nthis.adjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nprint(): void {\nconsole.log(\"\u9876\u70b9\u5217\u8868 = \", this.vertices);\nconsole.log(\"\u90bb\u63a5\u77e9\u9635 =\", this.adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.c<pre><code>[class]{graphAdjMat}-[func]{}\n</code></pre> graph_adjacency_matrix.cs<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat\n{\nList<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nList<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjMat(int[] vertices, int[][] edges)\n{\nthis.vertices = new List<int>();\nthis.adjMat = new List<List<int>>();\n// \u6dfb\u52a0\u9876\u70b9\nforeach (int val in vertices)\n{\naddVertex(val);\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nforeach (int[] e in edges)\n{\naddEdge(e[0], e[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size()\n{\nreturn vertices.Count;\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(int val)\n{\nint n = size();\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.Add(val);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nList<int> newRow = new List<int>(n);\nfor (int j = 0; j < n; j++)\n{\nnewRow.Add(0);\n}\nadjMat.Add(newRow);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nforeach (List<int> row in adjMat)\n{\nrow.Add(0);\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(int index)\n{\nif (index >= size())\nthrow new IndexOutOfRangeException();\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.RemoveAt(index);\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nforeach (List<int> row in adjMat)\n{\nrow.RemoveAt(index);\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void addEdge(int i, int j)\n{\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfRangeException();\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1;\nadjMat[j][i] = 1;\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\npublic void removeEdge(int i, int j)\n{\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\nthrow new IndexOutOfRangeException();\nadjMat[i][j] = 0;\nadjMat[j][i] = 0;\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\npublic void print()\n{\nConsole.Write(\"\u9876\u70b9\u5217\u8868 = \");\nPrintUtil.PrintList(vertices);\nConsole.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\nPrintUtil.printMatrix(adjMat);\n}\n}\n</code></pre> graph_adjacency_matrix.swift<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\nprivate var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\nprivate var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(vertices: [Int], edges: [[Int]]) {\nself.vertices = []\nadjMat = []\n// \u6dfb\u52a0\u9876\u70b9\nfor val in vertices {\naddVertex(val: val)\n}\n// \u6dfb\u52a0\u8fb9\n// \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfor e in edges {\naddEdge(i: e[0], j: e[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc size() -> Int {\nvertices.count\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc addVertex(val: Int) {\nlet n = size()\n// \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\nvertices.append(val)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\nlet newRow = Array(repeating: 0, count: n)\nadjMat.append(newRow)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\nfor i in adjMat.indices {\nadjMat[i].append(0)\n}\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc removeVertex(index: Int) {\nif index >= size() {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\nvertices.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\nadjMat.remove(at: index)\n// \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\nfor i in adjMat.indices {\nadjMat[i].remove(at: index)\n}\n}\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc addEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= size() || j >= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\n// \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u6cbf\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\nadjMat[i][j] = 1\nadjMat[j][i] = 1\n}\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc removeEdge(i: Int, j: Int) {\n// \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\nif i < 0 || j < 0 || i >= size() || j >= size() || i == j {\nfatalError(\"\u8d8a\u754c\")\n}\nadjMat[i][j] = 0\nadjMat[j][i] = 0\n}\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc print() {\nSwift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\nSwift.print(vertices)\nSwift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\nPrintUtil.printMatrix(matrix: adjMat)\n}\n}\n</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\u56fe\u7684\u9876\u70b9\u603b\u6570\u4e3a \\(n\\) \u3001\u8fb9\u603b\u6570\u4e3a \\(m\\) \uff0c\u5219\u6709\uff1a</p> <ul> <li>\u6dfb\u52a0\u8fb9\uff1a\u5728\u9876\u70b9\u5bf9\u5e94\u94fe\u8868\u7684\u5c3e\u90e8\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\u8be2\u4e0e\u5220\u9664\u6307\u5b9a\u8fb9\uff0c\u4f7f\u7528 \\(O(m)\\) \u65f6\u95f4\u3002\u4e0e\u6dfb\u52a0\u8fb9\u4e00\u6837\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\u5373\u53ef\uff0c\u5e76\u4ee5\u65b0\u589e\u9876\u70b9\u4e3a\u94fe\u8868\u5934\u7ed3\u70b9\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002</li> <li>\u5220\u9664\u9876\u70b9\uff1a\u9700\u8981\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\u9700\u8981\u5728\u90bb\u63a5\u8868\u4e2d\u5efa\u7acb \\(n\\) \u4e2a\u7ed3\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>\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u56fe\u7684\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u6ce8\u610f\u5230\uff0c\u6211\u4eec\u5728\u90bb\u63a5\u8868\u4e2d\u4f7f\u7528 <code>Vertex</code> \u7ed3\u70b9\u7c7b\u6765\u8868\u793a\u9876\u70b9\uff0c\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\uff1a</p> <ul> <li>\u5982\u679c\u6211\u4eec\u9009\u62e9\u901a\u8fc7\u9876\u70b9\u503c\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\uff0c\u90a3\u4e48\u503c\u91cd\u590d\u7684\u9876\u70b9\u5c06\u65e0\u6cd5\u88ab\u533a\u5206\u3002</li> <li>\u5982\u679c\u7c7b\u4f3c\u90bb\u63a5\u77e9\u9635\u90a3\u6837\uff0c\u4f7f\u7528\u9876\u70b9\u5217\u8868\u7d22\u5f15\u6765\u533a\u5206\u4e0d\u540c\u9876\u70b9\u3002\u90a3\u4e48\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5220\u9664\u7d22\u5f15\u4e3a \\(i\\) \u7684\u9876\u70b9\uff0c\u5219\u9700\u8981\u904d\u5386\u6574\u4e2a\u90bb\u63a5\u8868\uff0c\u5c06\u5176\u4e2d \\(> i\\) \u7684\u7d22\u5f15\u5168\u90e8\u6267\u884c \\(-1\\) \uff0c\u8fd9\u6837\u64cd\u4f5c\u6548\u7387\u592a\u4f4e\u3002</li> <li>\u56e0\u6b64\u6211\u4eec\u8003\u8651\u5f15\u5165\u9876\u70b9\u7c7b <code>Vertex</code> \uff0c\u4f7f\u5f97\u6bcf\u4e2a\u9876\u70b9\u90fd\u662f\u552f\u4e00\u7684\u5bf9\u8c61\uff0c\u6b64\u65f6\u5220\u9664\u9876\u70b9\u65f6\u5c31\u65e0\u9700\u6539\u52a8\u5176\u4f59\u9876\u70b9\u4e86\u3002</li> </ul> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig graph_adjacency_list.java<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nMap<Vertex, List<Vertex>> adjList;\n/* \u6784\u9020\u65b9\u6cd5 */\npublic GraphAdjList(Vertex[][] edges) {\nthis.adjList = new HashMap<>();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (Vertex[] edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size() {\nreturn adjList.size();\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList.get(vet1).add(vet2);\nadjList.get(vet2).add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2) {\nif (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\nthrow new IllegalArgumentException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList.get(vet1).remove(vet2);\nadjList.get(vet2).remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet) {\nif (adjList.containsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.put(vet, new ArrayList<>());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet) {\nif (!adjList.containsKey(vet))\nthrow new IllegalArgumentException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.remove(vet);\n// \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (List<Vertex> list : adjList.values()) {\nlist.remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print() {\nSystem.out.println(\"\u90bb\u63a5\u8868 =\");\nfor (Map.Entry<Vertex, List<Vertex>> entry : adjList.entrySet()) {\nList<Integer> tmp = new ArrayList<>();\nfor (Vertex vertex : entry.getValue())\ntmp.add(vertex.val);\nSystem.out.println(entry.getKey().val + \": \" + tmp + \",\");\n}\n}\n}\n</code></pre> graph_adjacency_list.cpp<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u4ee3\u66ff\u94fe\u8868\uff0c\u4ee5\u63d0\u5347\u5220\u9664\u8fb9\u3001\u5220\u9664\u9876\u70b9\u7684\u6548\u7387\n// \u8bf7\u6ce8\u610f\uff0cadjList \u4e2d\u7684\u5143\u7d20\u662f Vertex \u5bf9\u8c61\nunordered_map<Vertex*, unordered_set<Vertex*>> adjList;\npublic:\n/* \u6784\u9020\u65b9\u6cd5 */\nGraphAdjList(const vector<vector<Vertex*>>& edges) {\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const vector<Vertex*>& edge : edges) {\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nint size() { return adjList.size(); }\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(Vertex* vet1, Vertex* vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].insert(vet2);\nadjList[vet2].insert(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(Vertex* vet1, Vertex* vet2) {\nif (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1].erase(vet2);\nadjList[vet2].erase(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(Vertex* vet) {\nif (adjList.count(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = unordered_set<Vertex*>();\n}\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(Vertex* vet) {\nif (!adjList.count(vet))\nthrow invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.erase(vet);\n// \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (auto& [key, set_] : adjList) {\nset_.erase(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nvoid print() {\ncout << \"\u90bb\u63a5\u8868 =\" << endl;\nfor (auto& [key, value] : adjList) {\nvector<int> tmp;\nfor (Vertex* vertex : value)\ntmp.push_back(vertex->val);\ncout << key->val << \": \";\nPrintUtil::printVector(tmp);\n}\n}\n};\n</code></pre> graph_adjacency_list.py<pre><code>\"\"\" \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b \"\"\"\nclass GraphAdjList:\n# \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadj_list = {}\n\"\"\" \u6784\u9020\u65b9\u6cd5 \"\"\"\ndef __init__(self, edges: List[List[Vertex]]) -> None:\nself.adj_list = {}\n# \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges:\nself.add_vertex(edge[0])\nself.add_vertex(edge[1])\nself.add_edge(edge[0], edge[1])\n\"\"\" \u83b7\u53d6\u9876\u70b9\u6570\u91cf \"\"\"\ndef size(self) -> int:\nreturn len(self.adj_list)\n\"\"\" \u6dfb\u52a0\u8fb9 \"\"\"\ndef add_edge(self, vet1: Vertex, vet2: Vertex) -> None:\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError\n# \u6dfb\u52a0\u8fb9 vet1 - vet2\nself.adj_list[vet1].append(vet2)\nself.adj_list[vet2].append(vet1)\n\"\"\" \u5220\u9664\u8fb9 \"\"\"\ndef remove_edge(self, vet1: Vertex, vet2: Vertex) -> None:\nif vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\nraise ValueError\n# \u5220\u9664\u8fb9 vet1 - vet2\nself.adj_list[vet1].remove(vet2)\nself.adj_list[vet2].remove(vet1)\n\"\"\" \u6dfb\u52a0\u9876\u70b9 \"\"\"\ndef add_vertex(self, vet: Vertex) -> None:\nif vet in self.adj_list:\nreturn\n# \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nself.adj_list[vet] = []\n\"\"\" \u5220\u9664\u9876\u70b9 \"\"\"\ndef remove_vertex(self, vet: Vertex) -> None:\nif vet not in self.adj_list:\nraise ValueError\n# \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nself.adj_list.pop(vet)\n# \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor vertex in self.adj_list:\nif vet in self.adj_list[vertex]:\nself.adj_list[vertex].remove(vet)\n\"\"\" \u6253\u5370\u90bb\u63a5\u8868 \"\"\"\ndef print(self) -> None:\nprint(\"\u90bb\u63a5\u8868 =\")\nfor vertex in self.adj_list:\ntmp = [v.val for v in self.adj_list[vertex]]\nprint(f\"{vertex.val}: {tmp},\")\n</code></pre> graph_adjacency_list.go<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n// \u90bb\u63a5\u8868\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u4ee3\u66ff\u94fe\u8868\uff0c\u4ee5\u63d0\u5347\u5220\u9664\u8fb9\u3001\u5220\u9664\u9876\u70b9\u7684\u6548\u7387\n// \u8bf7\u6ce8\u610f\uff0cadjList \u4e2d\u7684\u5143\u7d20\u662f Vertex \u5bf9\u8c61\nadjList map[vertex]map[vertex]struct{}\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newGraphAdjList(edges [][]vertex) *graphAdjList {\ng := &graphAdjList{\nadjList: make(map[vertex]map[vertex]struct{}),\n}\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor _, edge := range edges {\ng.addVertex(edge[0])\ng.addVertex(edge[1])\ng.addEdge(edge[0], edge[1])\n}\nreturn g\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\nreturn len(g.adjList)\n}\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 vertex, vet2 vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\ng.adjList[vet1][vet2] = struct{}{}\ng.adjList[vet2][vet1] = struct{}{}\n}\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 vertex, vet2 vertex) {\n_, ok1 := g.adjList[vet1]\n_, ok2 := g.adjList[vet2]\nif !ok1 || !ok2 || vet1 == vet2 {\npanic(\"error\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2, \u501f\u52a9 delete \u6765\u5220\u9664 map \u4e2d\u7684\u952e\ndelete(g.adjList[vet1], vet2)\ndelete(g.adjList[vet2], vet1)\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet vertex) {\n_, ok := g.adjList[vet]\nif ok {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\ng.adjList[vet] = make(map[vertex]struct{})\n}\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet vertex) {\n_, ok := g.adjList[vet]\nif !ok {\npanic(\"error\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\ndelete(g.adjList, vet)\n// \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor _, set := range g.adjList {\n// \u64cd\u4f5c\ndelete(set, vet)\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\nvar builder strings.Builder\nfmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\nfor k, v := range g.adjList {\nbuilder.WriteString(\"\\t\\t\" + strconv.Itoa(k.val) + \": \")\nfor vet := range v {\nbuilder.WriteString(strconv.Itoa(vet.val) + \" \")\n}\nfmt.Println(builder.String())\nbuilder.Reset()\n}\n}\n</code></pre> graph_adjacency_list.js<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize() {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1, vet2) {\nif (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {\nthrow new Error(\"Illegal Argument Exception\");\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1, vet2) {\nif (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {\nthrow new Error(\"Illegal Argument Exception\");\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet) {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet) {\nif (!this.adjList.has(vet)) {\nthrow new Error(\"Illegal Argument Exception\");\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (let set of this.adjList.values()) {\nconst index = set.indexOf(vet);\nif (index > -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint() {\nconsole.log(\"\u90bb\u63a5\u8868 =\");\nfor (const [key, value] of this.adjList) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.log(key.val + \": \" + tmp.join());\n}\n}\n}\n</code></pre> graph_adjacency_list.ts<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nadjList: Map<Vertex, Vertex[]>;\n/* \u6784\u9020\u65b9\u6cd5 */\nconstructor(edges: Vertex[][]) {\nthis.adjList = new Map();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor (const edge of edges) {\nthis.addVertex(edge[0]);\nthis.addVertex(edge[1]);\nthis.addEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nsize(): number {\nreturn this.adjList.size;\n}\n/* \u6dfb\u52a0\u8fb9 */\naddEdge(vet1: Vertex, vet2: Vertex): void {\nif (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {\nthrow new Error(\"Illegal Argument Exception\");\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).push(vet2);\nthis.adjList.get(vet2).push(vet1);\n}\n/* \u5220\u9664\u8fb9 */\nremoveEdge(vet1: Vertex, vet2: Vertex): void {\nif (!this.adjList.has(vet1) || !this.adjList.has(vet2) || vet1 === vet2) {\nthrow new Error(\"Illegal Argument Exception\");\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nthis.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\nthis.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\naddVertex(vet: Vertex): void {\nif (this.adjList.has(vet)) return;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nthis.adjList.set(vet, []);\n}\n/* \u5220\u9664\u9876\u70b9 */\nremoveVertex(vet: Vertex): void {\nif (!this.adjList.has(vet)) {\nthrow new Error(\"Illegal Argument Exception\");\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nthis.adjList.delete(vet);\n// \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor (let set of this.adjList.values()) {\nconst index: number = set.indexOf(vet);\nif (index > -1) {\nset.splice(index, 1);\n}\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nprint(): void {\nconsole.log(\"\u90bb\u63a5\u8868 =\");\nfor (const [key, value] of this.adjList.entries()) {\nconst tmp = [];\nfor (const vertex of value) {\ntmp.push(vertex.val);\n}\nconsole.log(key.val + \": \" + tmp.join());\n}\n}\n}\n</code></pre> graph_adjacency_list.c<pre><code>[class]{graphAdjList}-[func]{}\n</code></pre> graph_adjacency_list.cs<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList\n{\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nDictionary<Vertex, List<Vertex>> adjList;\n/* \u6784\u9020\u51fd\u6570 */\npublic GraphAdjList(Vertex[][] edges)\n{\nthis.adjList = new Dictionary<Vertex, List<Vertex>>();\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nforeach (Vertex[] edge in edges)\n{\naddVertex(edge[0]);\naddVertex(edge[1]);\naddEdge(edge[0], edge[1]);\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic int size()\n{\nreturn adjList.Count;\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic void addEdge(Vertex vet1, Vertex vet2)\n{\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1].Add(vet2);\nadjList[vet2].Add(vet1);\n}\n/* \u5220\u9664\u8fb9 */\npublic void removeEdge(Vertex vet1, Vertex vet2)\n{\nif (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\nthrow new InvalidOperationException();\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1].Remove(vet2);\nadjList[vet2].Remove(vet1);\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic void addVertex(Vertex vet)\n{\nif (adjList.ContainsKey(vet))\nreturn;\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList.Add(vet, new List<Vertex>());\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic void removeVertex(Vertex vet)\n{\nif (!adjList.ContainsKey(vet))\nthrow new InvalidOperationException();\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.Remove(vet);\n// \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nforeach (List<Vertex> list in adjList.Values)\n{\nlist.Remove(vet);\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic void print()\n{\nConsole.WriteLine(\"\u90bb\u63a5\u8868 =\");\nforeach (KeyValuePair<Vertex, List<Vertex>> entry in adjList)\n{\nList<int> tmp = new List<int>();\nforeach (Vertex vertex in entry.Value)\ntmp.Add(vertex.Val);\nConsole.WriteLine(entry.Key.Val + \": [\" + string.Join(\", \", tmp) + \"],\");\n}\n}\n}\n</code></pre> graph_adjacency_list.swift<pre><code>/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n// \u90bb\u63a5\u8868\uff0ckey: \u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\npublic private(set) var adjList: [Vertex: [Vertex]]\n/* \u6784\u9020\u65b9\u6cd5 */\npublic init(edges: [[Vertex]]) {\nadjList = [:]\n// \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\nfor edge in edges {\naddVertex(vet: edge[0])\naddVertex(vet: edge[1])\naddEdge(vet1: edge[0], vet2: edge[1])\n}\n}\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\npublic func size() -> Int {\nadjList.count\n}\n/* \u6dfb\u52a0\u8fb9 */\npublic func addEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u6dfb\u52a0\u8fb9 vet1 - vet2\nadjList[vet1]?.append(vet2)\nadjList[vet2]?.append(vet1)\n}\n/* \u5220\u9664\u8fb9 */\npublic func removeEdge(vet1: Vertex, vet2: Vertex) {\nif adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5220\u9664\u8fb9 vet1 - vet2\nadjList[vet1]?.removeAll(where: { $0 == vet2 })\nadjList[vet2]?.removeAll(where: { $0 == vet1 })\n}\n/* \u6dfb\u52a0\u9876\u70b9 */\npublic func addVertex(vet: Vertex) {\nif adjList[vet] != nil {\nreturn\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\nadjList[vet] = []\n}\n/* \u5220\u9664\u9876\u70b9 */\npublic func removeVertex(vet: Vertex) {\nif adjList[vet] == nil {\nfatalError(\"\u53c2\u6570\u9519\u8bef\")\n}\n// \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\nadjList.removeValue(forKey: vet)\n// \u904d\u5386\u5176\u5b83\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\nfor key in adjList.keys {\nadjList[key]?.removeAll(where: { $0 == vet })\n}\n}\n/* \u6253\u5370\u90bb\u63a5\u8868 */\npublic func print() {\nSwift.print(\"\u90bb\u63a5\u8868 =\")\nfor entry in adjList {\nvar tmp: [Int] = []\nfor vertex in entry.value {\ntmp.append(vertex.val)\n}\nSwift.print(\"\\(entry.key.val): \\(tmp),\")\n}\n}\n}\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\u4e0b\u8868\u4e3a\u90bb\u63a5\u77e9\u9635\u548c\u90bb\u63a5\u8868\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u6548\u7387\u5bf9\u6bd4\u3002</p> \u90bb\u63a5\u77e9\u9635 \u90bb\u63a5\u8868\uff08\u94fe\u8868\uff09 \u90bb\u63a5\u8868\uff08\u54c8\u5e0c\u8868\uff09 \u5224\u65ad\u662f\u5426\u90bb\u63a5 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u8fb9 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u8fb9 \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) \u6dfb\u52a0\u9876\u70b9 \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u5220\u9664\u9876\u70b9 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u5185\u5b58\u7a7a\u95f4\u5360\u7528 \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\) <p>\u89c2\u5bdf\u4e0a\u8868\uff0c\u8c8c\u4f3c\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\u603b\u7ed3\u4ee5\u4e0a\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</p>"},{"location":"chapter_graph/graph_traversal/","title":"9.3. \u00a0 \u56fe\u7684\u904d\u5386","text":"<p>\u56fe\u4e0e\u6811\u7684\u5173\u7cfb</p> <p>\u6811\u4ee3\u8868\u7684\u662f\u201c\u4e00\u5bf9\u591a\u201d\u7684\u5173\u7cfb\uff0c\u800c\u56fe\u5219\u81ea\u7531\u5ea6\u66f4\u9ad8\uff0c\u53ef\u4ee5\u4ee3\u8868\u4efb\u610f\u201c\u591a\u5bf9\u591a\u201d\u5173\u7cfb\u3002\u672c\u8d28\u4e0a\uff0c\u53ef\u4ee5\u628a\u6811\u770b\u4f5c\u662f\u56fe\u7684\u4e00\u7c7b\u7279\u4f8b\u3002\u90a3\u4e48\u663e\u7136\uff0c\u6811\u904d\u5386\u64cd\u4f5c\u4e5f\u662f\u56fe\u904d\u5386\u64cd\u4f5c\u7684\u4e00\u4e2a\u7279\u4f8b\uff0c\u4e24\u8005\u7684\u65b9\u6cd5\u662f\u975e\u5e38\u7c7b\u4f3c\u7684\uff0c\u5efa\u8bae\u4f60\u5728\u5b66\u4e60\u672c\u7ae0\u8282\u7684\u8fc7\u7a0b\u4e2d\u5c06\u4e24\u8005\u878d\u4f1a\u8d2f\u901a\u3002</p> <p>\u300c\u56fe\u300d\u4e0e\u300c\u6811\u300d\u90fd\u662f\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u90fd\u9700\u8981\u4f7f\u7528\u300c\u641c\u7d22\u7b97\u6cd5\u300d\u6765\u5b9e\u73b0\u904d\u5386\u64cd\u4f5c\u3002</p> <p>\u7c7b\u4f3c\u5730\uff0c\u56fe\u7684\u904d\u5386\u65b9\u5f0f\u4e5f\u5206\u4e3a\u4e24\u79cd\uff0c\u5373\u300c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 Breadth-First Traversal\u300d\u548c\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 Depth-First Travsersal\u300d\uff0c\u4e5f\u79f0\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\u4e3a 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\u4f18\u662f\u4e00\u79cd\u7531\u8fd1\u53ca\u8fdc\u7684\u904d\u5386\u65b9\u5f0f\uff0c\u4ece\u8ddd\u79bb\u6700\u8fd1\u7684\u9876\u70b9\u5f00\u59cb\u8bbf\u95ee\uff0c\u5e76\u4e00\u5c42\u5c42\u5411\u5916\u6269\u5f20\u3002\u5177\u4f53\u5730\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u5148\u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u968f\u540e\u904d\u5386\u4e0b\u4e2a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\uff0c\u4ee5\u6b64\u7c7b\u63a8\u2026\u2026</p> <p></p> <p> Fig. \u56fe\u7684\u5e7f\u5ea6\u4f18\u5148\u904d\u5386 </p>"},{"location":"chapter_graph/graph_traversal/#_1","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"<p>BFS \u5e38\u501f\u52a9\u300c\u961f\u5217\u300d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u5177\u6709\u201c\u5148\u5165\u5148\u51fa\u201d\u7684\u6027\u8d28\uff0c\u8fd9\u4e0e BFS \u201c\u7531\u8fd1\u53ca\u8fdc\u201d\u7684\u601d\u60f3\u662f\u5f02\u66f2\u540c\u5de5\u7684\u3002</p> <ol> <li>\u5c06\u904d\u5386\u8d77\u59cb\u9876\u70b9 <code>startVet</code> \u52a0\u5165\u961f\u5217\uff0c\u5e76\u5f00\u542f\u5faa\u73af\uff1b</li> <li>\u5728\u5faa\u73af\u7684\u6bcf\u8f6e\u8fed\u4ee3\u4e2d\uff0c\u5f39\u51fa\u961f\u9996\u9876\u70b9\u5f39\u51fa\u5e76\u8bb0\u5f55\u8bbf\u95ee\uff0c\u5e76\u5c06\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\u52a0\u5165\u5230\u961f\u5217\u5c3e\u90e8\uff1b</li> <li>\u5faa\u73af <code>2.</code> \uff0c\u76f4\u5230\u6240\u6709\u9876\u70b9\u8bbf\u95ee\u5b8c\u6210\u540e\u7ed3\u675f\uff1b</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\u7ed3\u70b9\u5df2\u88ab\u8bbf\u95ee\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig graph_bfs.java<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList<Vertex> res = new ArrayList<>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = new HashSet<>() {{ add(startVet); }};\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nQueue<Vertex> que = new LinkedList<>() {{ offer(startVet); }};\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (!que.isEmpty()) {\nVertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.offer(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.cpp<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.py<pre><code>\"\"\" \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS \"\"\"\n# \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\ndef graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> List[Vertex]:\n# \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set([start_vet])\n# \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nque = collections.deque([start_vet])\n# \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile len(que) > 0:\nvet = que.popleft() # \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adj_vet in graph.adj_list[vet]:\nif adj_vet in visited:\ncontinue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nque.append(adj_vet) # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n</code></pre> graph_bfs.go<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.js<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.ts<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set<Vertex> = new Set();\nvisited.add(startVet);\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nconst que = [startVet];\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile (que.length) {\nconst vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet) ?? []) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\nvisited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res;\n}\n</code></pre> graph_bfs.c<pre><code>[class]{}-[func]{graphBFS}\n</code></pre> graph_bfs.cs<pre><code>[class]{graph_bfs}-[func]{graphBFS}\n</code></pre> graph_bfs.swift<pre><code>/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 BFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set<Vertex> = [startVet]\n// \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\nvar que: [Vertex] = [startVet]\n// \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\nwhile !que.isEmpty {\nlet vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\nque.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\nvisited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n}\n}\n// \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\nreturn res\n}\n</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\u4ee5\u4e0b\u52a8\u753b\u56fe\u793a\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>\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\u201c\u7531\u8fd1\u53ca\u8fdc\u201d\uff0c\u800c\u591a\u4e2a\u76f8\u540c\u8ddd\u79bb\u7684\u9876\u70b9\u7684\u904d\u5386\u987a\u5e8f\u5141\u8bb8\u88ab\u4efb\u610f\u6253\u4e71\u3002\u4ee5\u4e0a\u56fe\u4e3a\u4f8b\uff0c\u9876\u70b9 \\(1\\) , \\(3\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3001\u9876\u70b9 \\(2\\) , \\(4\\) , \\(6\\) \u7684\u8bbf\u95ee\u987a\u5e8f\u4e5f\u53ef\u4ee5\u4efb\u610f\u4ea4\u6362\u3001\u4ee5\u6b64\u7c7b\u63a8\u2026\u2026</p>"},{"location":"chapter_graph/graph_traversal/#_2","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u4f1a\u5165\u961f\u3001\u51fa\u961f\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(|V|)\\) \u65f6\u95f4\uff1b\u5728\u904d\u5386\u90bb\u63a5\u9876\u70b9\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7531\u4e8e\u662f\u65e0\u5411\u56fe\uff0c\u56e0\u6b64\u6240\u6709\u8fb9\u90fd\u4f1a\u88ab\u8bbf\u95ee \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 <code>res</code> \uff0c\u54c8\u5e0c\u8868 <code>visited</code> \uff0c\u961f\u5217 <code>que</code> \u4e2d\u7684\u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002</p>"},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2. \u00a0 \u6df1\u5ea6\u4f18\u5148\u904d\u5386","text":"<p>\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u662f\u4e00\u79cd\u4f18\u5148\u8d70\u5230\u5e95\u3001\u65e0\u8def\u53ef\u8d70\u518d\u56de\u5934\u7684\u904d\u5386\u65b9\u5f0f\u3002\u5177\u4f53\u5730\uff0c\u4ece\u67d0\u4e2a\u9876\u70b9\u51fa\u53d1\uff0c\u4e0d\u65ad\u5730\u8bbf\u95ee\u5f53\u524d\u7ed3\u70b9\u7684\u67d0\u4e2a\u90bb\u63a5\u9876\u70b9\uff0c\u76f4\u5230\u8d70\u5230\u5c3d\u5934\u65f6\u56de\u6eaf\uff0c\u518d\u7ee7\u7eed\u8d70\u5230\u5e95 + \u56de\u6eaf\uff0c\u4ee5\u6b64\u7c7b\u63a8\u2026\u2026\u76f4\u81f3\u6240\u6709\u9876\u70b9\u904d\u5386\u5b8c\u6210\u65f6\u7ed3\u675f\u3002</p> <p></p> <p> Fig. \u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u904d\u5386 </p>"},{"location":"chapter_graph/graph_traversal/#_3","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"<p>\u8fd9\u79cd\u201c\u8d70\u5230\u5934 + \u56de\u6eaf\u201d\u7684\u7b97\u6cd5\u5f62\u5f0f\u4e00\u822c\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002\u4e0e BFS \u7c7b\u4f3c\uff0c\u5728 DFS \u4e2d\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868 <code>visited</code> \u6765\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\uff0c\u4ee5\u907f\u514d\u91cd\u590d\u8bbf\u95ee\u9876\u70b9\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig graph_dfs.java<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\nres.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (Vertex adjVet : graph.adjList.get(vet)) {\nif (visited.contains(adjVet))\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nList<Vertex> res = new ArrayList<>();\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nSet<Vertex> visited = new HashSet<>();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.cpp<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.py<pre><code>\"\"\" \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 \"\"\"\ndef dfs(graph: GraphAdjList, visited: Set[Vertex], res: List[Vertex], vet: Vertex):\nres.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n# \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adj_list[vet]:\nif adjVet in visited:\ncontinue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n# \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet)\n\"\"\" \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \"\"\"\n# \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> List[Vertex]:\n# \u9876\u70b9\u904d\u5386\u5e8f\u5217\nres = []\n# \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvisited = set()\ndfs(graph, visited, res, start_vet)\nreturn res\n</code></pre> graph_dfs.go<pre><code>[class]{}-[func]{dfs}\n[class]{}-[func]{graphDFS}\n</code></pre> graph_dfs.js<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.ts<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunction dfs(graph: GraphAdjList, visited: Set<Vertex>, res: Vertex[], vet: Vertex): void {\nres.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor (const adjVet of graph.adjList.get(vet)) {\nif (visited.has(adjVet)) {\ncontinue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph, visited, res, adjVet);\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nconst res: Vertex[] = [];\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nconst visited: Set<Vertex> = new Set();\ndfs(graph, visited, res, startVet);\nreturn res;\n}\n</code></pre> graph_dfs.c<pre><code>[class]{}-[func]{dfs}\n[class]{}-[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.swift<pre><code>/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS \u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\nres.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\nvisited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n// \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfor adjVet in graph.adjList[vet] ?? [] {\nif visited.contains(adjVet) {\ncontinue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n}\n// \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\ndfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n}\n}\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 DFS */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n// \u9876\u70b9\u904d\u5386\u5e8f\u5217\nvar res: [Vertex] = []\n// \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\nvar visited: Set<Vertex> = []\ndfs(graph: graph, visited: &visited, res: &res, vet: startVet)\nreturn res\n}\n</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\u4e0b\u56fe\u6240\u793a\uff0c\u5176\u4e2d</p> <ul> <li>\u76f4\u865a\u7ebf\u4ee3\u8868\u5411\u4e0b\u9012\u63a8\uff0c\u4ee3\u8868\u5f00\u542f\u4e86\u4e00\u4e2a\u65b0\u7684\u9012\u5f52\u65b9\u6cd5\u6765\u8bbf\u95ee\u65b0\u9876\u70b9\uff1b</li> <li>\u66f2\u865a\u7ebf\u4ee3\u8868\u5411\u4e0a\u56de\u6eaf\uff0c\u4ee3\u8868\u6b64\u9012\u5f52\u65b9\u6cd5\u5df2\u7ecf\u8fd4\u56de\uff0c\u56de\u6eaf\u5230\u4e86\u5f00\u542f\u6b64\u9012\u5f52\u65b9\u6cd5\u7684\u4f4d\u7f6e\uff1b</li> </ul> <p>\u4e3a\u4e86\u52a0\u6df1\u7406\u89e3\uff0c\u8bf7\u4f60\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>\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\u884c\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\u4f53\u73b0\u4e09\u79cd\u4e0d\u540c\u7684\u904d\u5386\u4f18\u5148\u7ea7\uff0c\u800c\u4e09\u8005\u90fd\u5c5e\u4e8e\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002</p>"},{"location":"chapter_graph/graph_traversal/#_4","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a \u6240\u6709\u9876\u70b9\u90fd\u88ab\u8bbf\u95ee\u4e00\u6b21\uff1b\u6240\u6709\u8fb9\u90fd\u88ab\u8bbf\u95ee\u4e86 \\(2\\) \u6b21\uff0c\u4f7f\u7528 \\(O(2|E|)\\) \u65f6\u95f4\uff1b\u603b\u4f53\u4f7f\u7528 \\(O(|V| + |E|)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a \u5217\u8868 <code>res</code> \uff0c\u54c8\u5e0c\u8868 <code>visited</code> \u9876\u70b9\u6570\u91cf\u6700\u591a\u4e3a \\(|V|\\) \uff0c\u9012\u5f52\u6df1\u5ea6\u6700\u5927\u4e3a \\(|V|\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(|V|)\\) \u7a7a\u95f4\u3002</p>"},{"location":"chapter_graph/summary/","title":"9.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u56fe\u7531\u9876\u70b9\u548c\u8fb9\u7ec4\u6210\uff0c\u53ef\u4ee5\u8868\u793a\u4e3a\u4e00\u7ec4\u9876\u70b9\u548c\u4e00\u7ec4\u8fb9\u6784\u6210\u7684\u96c6\u5408\u3002</li> <li>\u76f8\u6bd4\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\u4e5f\u4ece\u800c\u66f4\u4e3a\u590d\u6742\u3002</li> <li>\u6709\u5411\u56fe\u7684\u8fb9\u5b58\u5728\u65b9\u5411\uff0c\u8fde\u901a\u56fe\u4e2d\u7684\u4efb\u610f\u9876\u70b9\u90fd\u53ef\u8fbe\uff0c\u6709\u6743\u56fe\u7684\u6bcf\u6761\u8fb9\u90fd\u5305\u542b\u6743\u91cd\u53d8\u91cf\u3002</li> <li>\u90bb\u63a5\u77e9\u9635\u4f7f\u7528\u65b9\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\u4f7f\u7528 \\(1\\) \u6216 \\(0\\) \u6765\u8868\u793a\u4e24\u4e2a\u9876\u70b9\u4e4b\u95f4\u6709\u8fb9\u6216\u65e0\u8fb9\u3002\u90bb\u63a5\u77e9\u9635\u7684\u589e\u5220\u67e5\u64cd\u4f5c\u6548\u7387\u5f88\u9ad8\uff0c\u4f46\u5360\u7528\u7a7a\u95f4\u5927\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\u90bb\u63a5\u77e9\u9635\u66f4\u52a0\u8282\u7701\u7a7a\u95f4\uff0c\u4f46\u7531\u4e8e\u9700\u8981\u901a\u8fc7\u904d\u5386\u94fe\u8868\u6765\u67e5\u627e\u8fb9\uff0c\u56e0\u6b64\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\u5316\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</li> <li>\u56fe\u53ef\u4ee5\u7528\u4e8e\u5efa\u6a21\u5404\u7c7b\u73b0\u5b9e\u7cfb\u7edf\uff0c\u4f8b\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\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\u518d\u56de\u5934\u7684\u641c\u7d22\u65b9\u5f0f\uff0c\u5e38\u57fa\u4e8e\u9012\u5f52\u6765\u5b9e\u73b0\u3002</li> </ul>"},{"location":"chapter_hashing/hash_collision/","title":"6.2. \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"<p>\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u54c8\u5e0c\u51fd\u6570\u5e94\u8be5\u4e3a\u6bcf\u4e2a\u8f93\u5165\u4ea7\u751f\u552f\u4e00\u7684\u8f93\u51fa\uff0c\u4f7f\u5f97 key \u548c value \u4e00\u4e00\u5bf9\u5e94\u3002\u800c\u5b9e\u9645\u4e0a\uff0c\u5f80\u5f80\u5b58\u5728\u5411\u54c8\u5e0c\u51fd\u6570\u8f93\u5165\u4e0d\u540c\u7684 key \u800c\u4ea7\u751f\u76f8\u540c\u8f93\u51fa\u7684\u60c5\u51b5\uff0c\u8fd9\u79cd\u60c5\u51b5\u88ab\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 Hash Collision\u300d\u3002\u54c8\u5e0c\u51b2\u7a81\u4f1a\u5bfc\u81f4\u67e5\u8be2\u7ed3\u679c\u9519\u8bef\uff0c\u4ece\u800c\u4e25\u91cd\u5f71\u54cd\u54c8\u5e0c\u8868\u7684\u53ef\u7528\u6027\u3002</p> <p>\u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\u5462\uff1f\u672c\u8d28\u4e0a\u770b\uff0c\u7531\u4e8e\u54c8\u5e0c\u51fd\u6570\u7684\u8f93\u5165\u7a7a\u95f4\u5f80\u5f80\u8fdc\u5927\u4e8e\u8f93\u51fa\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e0d\u53ef\u907f\u514d\u5730\u4f1a\u51fa\u73b0\u591a\u4e2a\u8f93\u5165\u4ea7\u751f\u76f8\u540c\u8f93\u51fa\u7684\u60c5\u51b5\uff0c\u5373\u4e3a\u54c8\u5e0c\u51b2\u7a81\u3002\u6bd4\u5982\uff0c\u8f93\u5165\u7a7a\u95f4\u662f\u5168\u4f53\u6574\u6570\uff0c\u8f93\u51fa\u7a7a\u95f4\u662f\u4e00\u4e2a\u56fa\u5b9a\u5927\u5c0f\u7684\u6876\uff08\u6570\u7ec4\uff09\u7684\u7d22\u5f15\u8303\u56f4\uff0c\u90a3\u4e48\u5fc5\u5b9a\u4f1a\u6709\u591a\u4e2a\u6574\u6570\u540c\u65f6\u6620\u5c04\u5230\u4e00\u4e2a\u6876\u7d22\u5f15\u3002</p> <p>\u4e3a\u4e86\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff0c\u4e00\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u54c8\u5e0c\u8868\u6269\u5bb9\u6765\u51cf\u5c0f\u51b2\u7a81\u6982\u7387\u3002\u6781\u7aef\u60c5\u51b5\u4e0b\uff0c\u5f53\u8f93\u5165\u7a7a\u95f4\u548c\u8f93\u51fa\u7a7a\u95f4\u5927\u5c0f\u76f8\u7b49\u65f6\uff0c\u54c8\u5e0c\u8868\u5c31\u7b49\u4ef7\u4e8e\u6570\u7ec4\u4e86\uff0c\u53ef\u8c13\u201c\u5927\u529b\u51fa\u5947\u8ff9\u201d\u3002</p> <p>\u53e6\u4e00\u65b9\u9762\uff0c\u8003\u8651\u901a\u8fc7\u4f18\u5316\u54c8\u5e0c\u8868\u7684\u8868\u793a\u65b9\u5f0f\u4ee5\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u300c\u94fe\u5f0f\u5730\u5740\u300d\u548c\u300c\u5f00\u653e\u5bfb\u5740\u300d\u3002</p>"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1. \u00a0 \u54c8\u5e0c\u8868\u6269\u5bb9","text":"<p>\u300c\u8d1f\u8f7d\u56e0\u5b50 Load Factor\u300d\u5b9a\u4e49\u4e3a \u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u69fd\u6570\u91cf\uff08\u5373\u6570\u7ec4\u5927\u5c0f\uff09\uff0c\u4ee3\u8868\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\u3002</p> <p>\u8d1f\u8f7d\u56e0\u5b50\u5e38\u7528\u4f5c\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u6bd4\u5982\u5728 Java \u4e2d\uff0c\u5f53\u8d1f\u8f7d\u56e0\u5b50 \\(> 0.75\\) \u65f6\u5219\u89e6\u53d1\u6269\u5bb9\uff0c\u5c06 HashMap \u5927\u5c0f\u6269\u5145\u81f3\u539f\u5148\u7684 \\(2\\) \u500d\u3002</p> <p>\u4e0e\u6570\u7ec4\u6269\u5bb9\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u7684\u5f00\u9500\u5f88\u5927\uff0c\u56e0\u4e3a\u9700\u8981\u5c06\u6240\u6709\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u4f9d\u6b21\u79fb\u52a8\u81f3\u65b0\u54c8\u5e0c\u8868\u3002</p>"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2. \u00a0 \u94fe\u5f0f\u5730\u5740","text":"<p>\u5728\u539f\u59cb\u54c8\u5e0c\u8868\u4e2d\uff0c\u6876\u5185\u7684\u6bcf\u4e2a\u5730\u5740\u53ea\u80fd\u5b58\u50a8\u4e00\u4e2a\u5143\u7d20\uff08\u5373\u952e\u503c\u5bf9\uff09\u3002\u8003\u8651\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u5316\u6210\u4e00\u4e2a\u94fe\u8868\uff0c\u5c06\u6240\u6709\u51b2\u7a81\u5143\u7d20\u90fd\u5b58\u50a8\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\u3002</p> <p></p> <p> Fig. \u94fe\u5f0f\u5730\u5740 </p> <p>\u94fe\u5f0f\u5730\u5740\u4e0b\uff0c\u54c8\u5e0c\u8868\u64cd\u4f5c\u65b9\u6cd5\u4e3a\uff1a</p> <ul> <li>\u67e5\u8be2\u5143\u7d20\uff1a\u5148\u5c06 key \u8f93\u5165\u5230\u54c8\u5e0c\u51fd\u6570\u5f97\u5230\u6876\u5185\u7d22\u5f15\uff0c\u5373\u53ef\u8bbf\u95ee\u94fe\u8868\u5934\u7ed3\u70b9\uff0c\u518d\u901a\u8fc7\u904d\u5386\u94fe\u8868\u67e5\u627e\u5bf9\u5e94 value \u3002</li> <li>\u6dfb\u52a0\u5143\u7d20\uff1a\u5148\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u518d\u5c06\u7ed3\u70b9\uff08\u5373\u952e\u503c\u5bf9\uff09\u6dfb\u52a0\u5230\u94fe\u8868\u5934\u90e8\u5373\u53ef\u3002</li> <li>\u5220\u9664\u5143\u7d20\uff1a\u540c\u6837\u5148\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u7ed3\u679c\u8bbf\u95ee\u94fe\u8868\u5934\u90e8\uff0c\u518d\u904d\u5386\u94fe\u8868\u67e5\u627e\u5bf9\u5e94\u7ed3\u70b9\uff0c\u5220\u9664\u4e4b\u5373\u53ef\u3002</li> </ul> <p>\u94fe\u5f0f\u5730\u5740\u867d\u7136\u89e3\u51b3\u4e86\u54c8\u5e0c\u51b2\u7a81\u95ee\u9898\uff0c\u4f46\u4ecd\u5b58\u5728\u5c40\u9650\u6027\uff0c\u5305\u62ec\uff1a</p> <ul> <li>\u5360\u7528\u7a7a\u95f4\u53d8\u5927\uff0c\u56e0\u4e3a\u94fe\u8868\u6216\u4e8c\u53c9\u6811\u5305\u542b\u7ed3\u70b9\u6307\u9488\uff0c\u76f8\u6bd4\u4e8e\u6570\u7ec4\u66f4\u52a0\u8017\u8d39\u5185\u5b58\u7a7a\u95f4\uff1b</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\uff1b</li> </ul> <p>\u4e3a\u4e86\u63d0\u5347\u64cd\u4f5c\u6548\u7387\uff0c\u53ef\u4ee5\u628a\u300c\u94fe\u8868\u300d\u8f6c\u5316\u4e3a\u300cAVL \u6811\u300d\u6216\u300c\u7ea2\u9ed1\u6811\u300d\uff0c\u5c06\u67e5\u8be2\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002</p>"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3. \u00a0 \u5f00\u653e\u5bfb\u5740","text":"<p>\u300c\u5f00\u653e\u5bfb\u5740\u300d\u4e0d\u5f15\u5165\u989d\u5916\u6570\u636e\u7ed3\u6784\uff0c\u800c\u662f\u901a\u8fc7\u201c\u591a\u6b21\u63a2\u6d4b\u201d\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u6839\u636e\u63a2\u6d4b\u65b9\u6cd5\u7684\u4e0d\u540c\uff0c\u4e3b\u8981\u5206\u4e3a \u7ebf\u6027\u63a2\u6d4b\u3001\u5e73\u65b9\u63a2\u6d4b\u3001\u591a\u6b21\u54c8\u5e0c\u3002</p>"},{"location":"chapter_hashing/hash_collision/#_1","title":"\u7ebf\u6027\u63a2\u6d4b","text":"<p>\u300c\u7ebf\u6027\u63a2\u6d4b\u300d\u4f7f\u7528\u56fa\u5b9a\u6b65\u957f\u7684\u7ebf\u6027\u67e5\u627e\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002</p> <p>\u63d2\u5165\u5143\u7d20\uff1a\u5982\u679c\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5411\u540e\u7ebf\u6027\u904d\u5386\uff08\u6b65\u957f\u4e00\u822c\u53d6 1 \uff09\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u7a7a\u4f4d\uff0c\u5219\u5c06\u5143\u7d20\u63d2\u5165\u5230\u8be5\u7a7a\u4f4d\u4e2d\u3002</p> <p>\u67e5\u627e\u5143\u7d20\uff1a\u82e5\u51fa\u73b0\u54c8\u5e0c\u51b2\u7a81\uff0c\u5219\u4f7f\u7528\u76f8\u540c\u6b65\u957f\u6267\u884c\u7ebf\u6027\u67e5\u627e\uff0c\u4f1a\u9047\u5230\u4e24\u79cd\u60c5\u51b5\uff1a</p> <ol> <li>\u627e\u5230\u5bf9\u5e94\u5143\u7d20\uff0c\u8fd4\u56de value \u5373\u53ef\uff1b</li> <li>\u82e5\u9047\u5230\u7a7a\u4f4d\uff0c\u5219\u8bf4\u660e\u67e5\u627e\u952e\u503c\u5bf9\u4e0d\u5728\u54c8\u5e0c\u8868\u4e2d\uff1b</li> </ol> <p></p> <p> Fig. \u7ebf\u6027\u63a2\u6d4b </p> <p>\u7ebf\u6027\u63a2\u6d4b\u5b58\u5728\u4ee5\u4e0b\u7f3a\u9677\uff1a</p> <ul> <li>\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u5143\u7d20\u3002\u5220\u9664\u5143\u7d20\u4f1a\u5bfc\u81f4\u6876\u5185\u51fa\u73b0\u4e00\u4e2a\u7a7a\u4f4d\uff0c\u5728\u67e5\u627e\u5176\u4ed6\u5143\u7d20\u65f6\uff0c\u8be5\u7a7a\u4f4d\u6709\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u8ba4\u4e3a\u5143\u7d20\u4e0d\u5b58\u5728\uff08\u5373\u4e0a\u8ff0\u7b2c <code>2.</code> \u79cd\u60c5\u51b5\uff09\u3002\u56e0\u6b64\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u6807\u5fd7\u4f4d\u6765\u6807\u8bb0\u5220\u9664\u5143\u7d20\u3002</li> <li>\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u6876\u5185\u88ab\u5360\u7528\u7684\u8fde\u7eed\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\u8fdb\u8fd9\u4e00\u4f4d\u7f6e\u7684\u201c\u805a\u5806\u751f\u957f\u201d\uff0c\u6700\u7ec8\u5bfc\u81f4\u589e\u5220\u67e5\u6539\u64cd\u4f5c\u6548\u7387\u7684\u52a3\u5316\u3002</li> </ul>"},{"location":"chapter_hashing/hash_collision/#_2","title":"\u591a\u6b21\u54c8\u5e0c","text":"<p>\u987e\u540d\u601d\u4e49\uff0c\u300c\u591a\u6b21\u54c8\u5e0c\u300d\u7684\u601d\u8def\u662f\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570 \\(f_1(x)\\) , \\(f_2(x)\\) , \\(f_3(x)\\) , \\(\\cdots\\) \u8fdb\u884c\u63a2\u6d4b\u3002</p> <p>\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\u2026\u2026\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u540e\u63d2\u5165\u5143\u7d20\u3002</p> <p>\u67e5\u627e\u5143\u7d20\uff1a\u4ee5\u76f8\u540c\u7684\u54c8\u5e0c\u51fd\u6570\u987a\u5e8f\u67e5\u627e\uff0c\u5b58\u5728\u4e24\u79cd\u60c5\u51b5\uff1a</p> <ol> <li>\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u4e4b\uff1b</li> <li>\u5230\u7a7a\u4f4d\u6216\u5df2\u5c1d\u8bd5\u6240\u6709\u54c8\u5e0c\u51fd\u6570\uff0c\u8bf4\u660e\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64\u5143\u7d20\uff1b</li> </ol> <p>\u76f8\u6bd4\u4e8e\u300c\u7ebf\u6027\u63a2\u6d4b\u300d\uff0c\u300c\u591a\u6b21\u54c8\u5e0c\u300d\u65b9\u6cd5\u66f4\u4e0d\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4ee3\u4ef7\u662f\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u589e\u52a0\u4e86\u989d\u5916\u8ba1\u7b97\u91cf\u3002</p> <p>\u5de5\u4e1a\u754c\u65b9\u6848</p> <p>Java \u91c7\u7528\u300c\u94fe\u5f0f\u5730\u5740\u300d\u3002\u5728 JDK 1.8 \u4e4b\u540e\uff0cHashMap \u5185\u6570\u7ec4\u957f\u5ea6\u5927\u4e8e 64 \u65f6\uff0c\u957f\u5ea6\u5927\u4e8e 8 \u7684\u94fe\u8868\u4f1a\u88ab\u8f6c\u5316\u4e3a\u300c\u7ea2\u9ed1\u6811\u300d\uff0c\u4ee5\u63d0\u5347\u67e5\u627e\u6027\u80fd\u3002</p> <p>Python \u91c7\u7528\u300c\u5f00\u653e\u5bfb\u5740\u300d\u3002\u5b57\u5178 dict \u4f7f\u7528\u4f2a\u968f\u673a\u6570\u8fdb\u884c\u63a2\u6d4b\u3002 </p>"},{"location":"chapter_hashing/hash_map/","title":"6.1. \u00a0 \u54c8\u5e0c\u8868\uff08Hash Map\uff09","text":"<p>\u54c8\u5e0c\u8868\u901a\u8fc7\u5efa\u7acb\u300c\u952e key\u300d\u548c\u300c\u503c value\u300d\u4e4b\u95f4\u7684\u6620\u5c04\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u627e\u3002\u5177\u4f53\u5730\uff0c\u8f93\u5165\u4e00\u4e2a key \uff0c\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u8be2\u5e76\u83b7\u53d6 value \uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \u3002</p> <p>\u4f8b\u5982\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b \\(n\\) \u4e2a\u5b66\u751f\u7684\u6570\u636e\u5e93\uff0c\u6bcf\u4e2a\u5b66\u751f\u6709\u201c\u59d3\u540d <code>name</code> \u201d\u548c\u201c\u5b66\u53f7 <code>id</code> \u201d\u4e24\u9879\u6570\u636e\uff0c\u5e0c\u671b\u5b9e\u73b0\u4e00\u4e2a\u67e5\u8be2\u529f\u80fd\uff1a\u8f93\u5165\u4e00\u4e2a\u5b66\u53f7\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\u54c8\u5e0c\u8868\u5b9e\u73b0\u3002</p> <p></p> <p> Fig. \u54c8\u5e0c\u8868\u7684\u62bd\u8c61\u8868\u793a </p>"},{"location":"chapter_hashing/hash_map/#611","title":"6.1.1. \u00a0 \u54c8\u5e0c\u8868\u6548\u7387","text":"<p>\u9664\u4e86\u54c8\u5e0c\u8868\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u4e0a\u8ff0\u67e5\u8be2\u529f\u80fd\uff1a</p> <ol> <li>\u65e0\u5e8f\u6570\u7ec4\uff1a\u6bcf\u4e2a\u5143\u7d20\u4e3a <code>[\u5b66\u53f7, \u59d3\u540d]</code> \uff1b</li> <li>\u6709\u5e8f\u6570\u7ec4\uff1a\u5c06 <code>1.</code> \u4e2d\u7684\u6570\u7ec4\u6309\u7167\u5b66\u53f7\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\uff1b</li> <li>\u94fe\u8868\uff1a\u6bcf\u4e2a\u7ed3\u70b9\u7684\u503c\u4e3a <code>[\u5b66\u53f7, \u59d3\u540d]</code> \uff1b</li> <li>\u4e8c\u53c9\u641c\u7d22\u6811\uff1a\u6bcf\u4e2a\u7ed3\u70b9\u7684\u503c\u4e3a <code>[\u5b66\u53f7, \u59d3\u540d]</code> \uff0c\u6839\u636e\u5b66\u53f7\u5927\u5c0f\u6765\u6784\u5efa\u6811\uff1b</li> </ol> <p>\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5982\u4e0b\u8868\u6240\u793a\uff08\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\uff0c\u8be6\u89e3\u53ef\u89c1 \u4e8c\u53c9\u641c\u7d22\u6811\u7ae0\u8282\uff09\u3002\u65e0\u8bba\u662f\u67e5\u627e\u5143\u7d20\u3001\u8fd8\u662f\u589e\u5220\u5143\u7d20\uff0c\u54c8\u5e0c\u8868\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f \\(O(1)\\) \uff0c\u5168\u9762\u80dc\u51fa\uff01</p> \u65e0\u5e8f\u6570\u7ec4 \u6709\u5e8f\u6570\u7ec4 \u94fe\u8868 \u4e8c\u53c9\u641c\u7d22\u6811 \u54c8\u5e0c\u8868 \u67e5\u627e\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) \\(O(\\log n)\\) \\(O(1)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\)"},{"location":"chapter_hashing/hash_map/#612","title":"6.1.2. \u00a0 \u54c8\u5e0c\u8868\u5e38\u7528\u64cd\u4f5c","text":"<p>\u54c8\u5e0c\u8868\u7684\u57fa\u672c\u64cd\u4f5c\u5305\u62ec \u521d\u59cb\u5316\u3001\u67e5\u8be2\u64cd\u4f5c\u3001\u6dfb\u52a0\u4e0e\u5220\u9664\u952e\u503c\u5bf9\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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.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.py<pre><code>\"\"\" \u521d\u59cb\u5316\u54c8\u5e0c\u8868 \"\"\"\nmapp = {}\n\"\"\" \u6dfb\u52a0\u64cd\u4f5c \"\"\"\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmapp[12836] = \"\u5c0f\u54c8\"\nmapp[15937] = \"\u5c0f\u5570\"\nmapp[16750] = \"\u5c0f\u7b97\"\nmapp[13276] = \"\u5c0f\u6cd5\"\nmapp[10583] = \"\u5c0f\u9e2d\"\n\"\"\" \u67e5\u8be2\u64cd\u4f5c \"\"\"\n# \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname = mapp[15937]\n\"\"\" \u5220\u9664\u64cd\u4f5c \"\"\"\n# \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmapp.pop(10583)\n</code></pre> hash_map.go<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nmapp := make(map[int]string)\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmapp[12836] = \"\u5c0f\u54c8\"\nmapp[15937] = \"\u5c0f\u5570\"\nmapp[16750] = \"\u5c0f\u7b97\"\nmapp[13276] = \"\u5c0f\u6cd5\"\nmapp[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nname := mapp[15937]\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\ndelete(mapp, 10583)\n</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.c<pre><code>\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.swift<pre><code>/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nvar map: [Int: String] = [:]\n/* \u6dfb\u52a0\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9 (key, value)\nmap[12836] = \"\u5c0f\u54c8\"\nmap[15937] = \"\u5c0f\u5570\"\nmap[16750] = \"\u5c0f\u7b97\"\nmap[13276] = \"\u5c0f\u6cd5\"\nmap[10583] = \"\u5c0f\u9e2d\"\n/* \u67e5\u8be2\u64cd\u4f5c */\n// \u5411\u54c8\u5e0c\u8868\u8f93\u5165\u952e key \uff0c\u5f97\u5230\u503c value\nlet name = map[15937]!\n/* \u5220\u9664\u64cd\u4f5c */\n// \u5728\u54c8\u5e0c\u8868\u4e2d\u5220\u9664\u952e\u503c\u5bf9 (key, value)\nmap.removeValue(forKey: 10583)\n</code></pre> hash_map.zig<pre><code>\n</code></pre> <p>\u904d\u5386\u54c8\u5e0c\u8868\u6709\u4e09\u79cd\u65b9\u5f0f\uff0c\u5373 \u904d\u5386\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u3001\u904d\u5386\u503c\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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.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 key: map) {\ncout << key.first << endl;\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor (auto val: map) {\ncout << val.second << endl;\n}\n</code></pre> hash_map.py<pre><code>\"\"\" \u904d\u5386\u54c8\u5e0c\u8868 \"\"\"\n# \u904d\u5386\u952e\u503c\u5bf9 key->value\nfor key, value in mapp.items():\nprint(key, \"->\", value)\n# \u5355\u72ec\u904d\u5386\u952e key\nfor key in mapp.keys():\nprint(key)\n# \u5355\u72ec\u904d\u5386\u503c value\nfor value in mapp.values():\nprint(value)\n</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 mapp {\nfmt.Println(key, \"->\", value)\n}\n// \u5355\u72ec\u904d\u5386\u952e key\nfor key := range mapp {\nfmt.Println(key)\n}\n// \u5355\u72ec\u904d\u5386\u503c value\nfor _, value := range mapp {\nfmt.Println(value)\n}\n</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.c<pre><code>\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.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.zig<pre><code>\n</code></pre>"},{"location":"chapter_hashing/hash_map/#613","title":"6.1.3. \u00a0 \u54c8\u5e0c\u51fd\u6570","text":"<p>\u54c8\u5e0c\u8868\u4e2d\u5b58\u50a8\u5143\u7d20\u7684\u6570\u636e\u7ed3\u6784\u88ab\u79f0\u4e3a\u300c\u6876 Bucket\u300d\uff0c\u5e95\u5c42\u5b9e\u73b0\u53ef\u80fd\u662f\u6570\u7ec4\u3001\u94fe\u8868\u3001\u4e8c\u53c9\u6811\uff08\u7ea2\u9ed1\u6811\uff09\uff0c\u6216\u662f\u5b83\u4eec\u7684\u7ec4\u5408\u3002</p> <p>\u6700\u7b80\u5355\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u4ec5\u7528\u4e00\u4e2a\u300c\u6570\u7ec4\u300d\u6765\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u9996\u5148\uff0c\u5c06\u6240\u6709 value \u653e\u5165\u6570\u7ec4\u4e2d\uff0c\u90a3\u4e48\u6bcf\u4e2a value \u5728\u6570\u7ec4\u4e2d\u90fd\u6709\u552f\u4e00\u7684\u300c\u7d22\u5f15\u300d\u3002\u663e\u7136\uff0c\u8bbf\u95ee value \u9700\u8981\u7ed9\u5b9a\u7d22\u5f15\uff0c\u800c\u4e3a\u4e86 \u5efa\u7acb key \u548c\u7d22\u5f15\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u300c\u54c8\u5e0c\u51fd\u6570 Hash Function\u300d\u3002</p> <p>\u8bbe\u6570\u7ec4\u4e3a <code>bucket</code> \uff0c\u54c8\u5e0c\u51fd\u6570\u4e3a <code>f(x)</code> \uff0c\u8f93\u5165\u952e\u4e3a <code>key</code> \u3002\u90a3\u4e48\u83b7\u53d6 value \u7684\u6b65\u9aa4\u4e3a\uff1a</p> <ol> <li>\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u51fa\u7d22\u5f15\uff0c\u5373 <code>index = f(key)</code> \uff1b</li> <li>\u901a\u8fc7\u7d22\u5f15\u5728\u6570\u7ec4\u4e2d\u83b7\u53d6\u503c\uff0c\u5373 <code>value = bucket[index]</code> \uff1b</li> </ol> <p>\u4ee5\u4e0a\u8ff0\u5b66\u751f\u6570\u636e <code>key \u5b66\u53f7 -> value \u59d3\u540d</code> \u4e3a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u300c\u54c8\u5e0c\u51fd\u6570\u300d\u8bbe\u8ba1\u4e3a</p> \\[ f(x) = x \\% 100 \\] <p></p> <p> Fig. \u7b80\u5355\u54c8\u5e0c\u51fd\u6570\u793a\u4f8b </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array_hash_map.java<pre><code>/* \u952e\u503c\u5bf9 int->String */\nclass Entry {\npublic int key;\npublic String val;\npublic Entry(int key, String val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate List<Entry> bucket;\npublic ArrayHashMap() {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nbucket = new ArrayList<>();\nfor (int i = 0; i < 100; i++) {\nbucket.add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String get(int key) {\nint index = hashFunc(key);\nEntry pair = bucket.get(index);\nif (pair == null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val) {\nEntry pair = new Entry(key, val);\nint index = hashFunc(key);\nbucket.set(index, pair);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbucket.set(index, null);\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List<Entry> entrySet() {\nList<Entry> entrySet = new ArrayList<>();\nfor (Entry pair : bucket) {\nif (pair != null)\nentrySet.add(pair);\n}\nreturn entrySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List<Integer> keySet() {\nList<Integer> keySet = new ArrayList<>();\nfor (Entry pair : bucket) {\nif (pair != null)\nkeySet.add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List<String> valueSet() {\nList<String> valueSet = new ArrayList<>();\nfor (Entry pair : bucket) {\nif (pair != null)\nvalueSet.add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print() {\nfor (Entry kv: entrySet()) {\nSystem.out.println(kv.key + \" -> \" + kv.val);\n}\n}\n}\n</code></pre> array_hash_map.cpp<pre><code>/* \u952e\u503c\u5bf9 int->String */\nstruct Entry {\npublic:\nint key;\nstring val;\nEntry(int key, string val) {\nthis->key = key;\nthis->val = val;\n}\n};\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate:\nvector<Entry*> bucket;\npublic:\nArrayHashMap() {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nbucket= vector<Entry*>(100);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(int key) {\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nstring get(int key) {\nint index = hashFunc(key);\nEntry* pair = bucket[index];\nif (pair == nullptr)\nreturn nullptr;\nreturn pair->val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(int key, string val) {\nEntry* pair = new Entry(key, val);\nint index = hashFunc(key);\nbucket[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\nvoid remove(int key) {\nint index = hashFunc(key);\n// \u7f6e\u4e3a nullptr \uff0c\u4ee3\u8868\u5220\u9664\nbucket[index] = nullptr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvector<Entry*> entrySet() {\nvector<Entry*> entrySet;\nfor (Entry* pair: bucket) {\nif (pair != nullptr) {\nentrySet.push_back(pair);\n}\n}\nreturn entrySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvector<int> keySet() {\nvector<int> keySet;\nfor (Entry* pair: bucket) {\nif (pair != nullptr) {\nkeySet.push_back(pair->key);\n}\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvector<string> valueSet() {\nvector<string> valueSet;\nfor (Entry* pair: bucket) {\nif (pair != nullptr){\nvalueSet.push_back(pair->val);\n}\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print() {\nfor (Entry* kv: entrySet()) {\ncout << kv->key << \" -> \" << kv->val << endl;\n}\n}\n};\n</code></pre> array_hash_map.py<pre><code>\"\"\" \u952e\u503c\u5bf9 int->String \"\"\"\nclass Entry:\ndef __init__(self, key, val):\nself.key = key\nself.val = val\n\"\"\" \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 \"\"\"\nclass ArrayHashMap:\ndef __init__(self):\n# \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nself.bucket = [None] * 100\n\"\"\" \u54c8\u5e0c\u51fd\u6570 \"\"\"\ndef hash_func(self, key):\nindex = key % 100\nreturn index\n\"\"\" \u67e5\u8be2\u64cd\u4f5c \"\"\"\ndef get(self, key):\nindex = self.hash_func(key)\npair = self.bucket[index]\nif pair is None:\nreturn None\nreturn pair.val\n\"\"\" \u6dfb\u52a0\u64cd\u4f5c \"\"\"\ndef put(self, key, val):\npair = Entry(key, val)\nindex = self.hash_func(key)\nself.bucket[index] = pair\n\"\"\" \u5220\u9664\u64cd\u4f5c \"\"\"\ndef remove(self, key):\nindex = self.hash_func(key)\n# \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\nself.bucket[index] = None\n\"\"\" \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 \"\"\"\ndef entry_set(self):\nresult = []\nfor pair in self.bucket:\nif pair is not None:\nresult.append(pair)\nreturn result\n\"\"\" \u83b7\u53d6\u6240\u6709\u952e \"\"\"\ndef key_set(self):\nresult = []\nfor pair in self.bucket:\nif pair is not None:\nresult.append(pair.key)\nreturn result\n\"\"\" \u83b7\u53d6\u6240\u6709\u503c \"\"\"\ndef value_set(self):\nresult = []\nfor pair in self.bucket:\nif pair is not None:\nresult.append(pair.val)\nreturn result\n\"\"\" \u6253\u5370\u54c8\u5e0c\u8868 \"\"\"\ndef print(self):\nfor pair in self.bucket:\nif pair is not None:\nprint(pair.key, \"->\", pair.val)\n</code></pre> array_hash_map.go<pre><code>/* \u952e\u503c\u5bf9 int->String */\ntype entry struct {\nkey int\nval string\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\nbucket []*entry\n}\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nbucket := make([]*entry, 100)\nreturn &arrayHashMap{bucket: bucket}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\nindex := key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\nindex := a.hashFunc(key)\npair := a.bucket[index]\nif pair == nil {\nreturn \"Not Found\"\n}\nreturn pair.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\npair := &entry{key: key, val: val}\nindex := a.hashFunc(key)\na.bucket[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\nindex := a.hashFunc(key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\na.bucket[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) entrySet() []*entry {\nvar pairs []*entry\nfor _, pair := range a.bucket {\nif pair != nil {\npairs = append(pairs, pair)\n}\n}\nreturn pairs\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\nvar keys []int\nfor _, pair := range a.bucket {\nif pair != nil {\nkeys = append(keys, pair.key)\n}\n}\nreturn keys\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\nvar values []string\nfor _, pair := range a.bucket {\nif pair != nil {\nvalues = append(values, pair.val)\n}\n}\nreturn values\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\nfor _, pair := range a.bucket {\nif pair != nil {\nfmt.Println(pair.key, \"->\", pair.val)\n}\n}\n}\n</code></pre> array_hash_map.js<pre><code>/* \u952e\u503c\u5bf9 Number -> String */\nclass Entry {\nconstructor(key, val) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n#bucket;\nconstructor() {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nthis.#bucket = new Array(100).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\n#hashFunc(key) {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nget(key) {\nlet index = this.#hashFunc(key);\nlet entry = this.#bucket[index];\nif (entry === null) return null;\nreturn entry.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nset(key, val) {\nlet index = this.#hashFunc(key);\nthis.#bucket[index] = new Entry(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\ndelete(key) {\nlet index = this.#hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.#bucket[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nentries() {\nlet arr = [];\nfor (let i = 0; i < this.#bucket.length; i++) {\nif (this.#bucket[i]) {\narr.push(this.#bucket[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nkeys() {\nlet arr = [];\nfor (let i = 0; i < this.#bucket.length; i++) {\nif (this.#bucket[i]) {\narr.push(this.#bucket[i]?.key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvalues() {\nlet arr = [];\nfor (let i = 0; i < this.#bucket.length; i++) {\nif (this.#bucket[i]) {\narr.push(this.#bucket[i]?.val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nprint() {\nlet entrySet = this.entries();\nfor (const entry of entrySet) {\nif (!entry) continue;\nconsole.info(`${entry.key} -> ${entry.val}`);\n}\n}\n}\n</code></pre> array_hash_map.ts<pre><code>/* \u952e\u503c\u5bf9 Number -> String */\nclass Entry {\npublic key: number;\npublic val: string;\nconstructor(key: number, val: string) {\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate readonly bucket: (Entry | null)[];\nconstructor() {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nthis.bucket = (new Array(100)).fill(null);\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate hashFunc(key: number): number {\nreturn key % 100;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic get(key: number): string | null {\nlet index = this.hashFunc(key);\nlet entry = this.bucket[index];\nif (entry === null) return null;\nreturn entry.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic set(key: number, val: string) {\nlet index = this.hashFunc(key);\nthis.bucket[index] = new Entry(key, val);\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic delete(key: number) {\nlet index = this.hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nthis.bucket[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic entries(): (Entry | null)[] {\nlet arr: (Entry | null)[] = [];\nfor (let i = 0; i < this.bucket.length; i++) {\nif (this.bucket[i]) {\narr.push(this.bucket[i]);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic keys(): (number | undefined)[] {\nlet arr: (number | undefined)[] = [];\nfor (let i = 0; i < this.bucket.length; i++) {\nif (this.bucket[i]) {\narr.push(this.bucket[i]?.key);\n}\n}\nreturn arr;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic values(): (string | undefined)[] {\nlet arr: (string | undefined)[] = [];\nfor (let i = 0; i < this.bucket.length; i++) {\nif (this.bucket[i]) {\narr.push(this.bucket[i]?.val);\n}\n}\nreturn arr;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic print() {\nlet entrySet = this.entries();\nfor (const entry of entrySet) {\nif (!entry) continue;\nconsole.info(`${entry.key} -> ${entry.val}`);\n}\n}\n}\n</code></pre> array_hash_map.c<pre><code>[class]{entry}-[func]{}\n[class]{arrayHashMap}-[func]{}\n</code></pre> array_hash_map.cs<pre><code>/* \u952e\u503c\u5bf9 int->String */\nclass Entry\n{\npublic int key;\npublic String val;\npublic Entry(int key, String val)\n{\nthis.key = key;\nthis.val = val;\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap\n{\nprivate List<Entry?> bucket;\npublic ArrayHashMap()\n{\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nbucket = new();\nfor (int i = 0; i < 100; i++)\n{\nbucket.Add(null);\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate int hashFunc(int key)\n{\nint index = key % 100;\nreturn index;\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\npublic String? get(int key)\n{\nint index = hashFunc(key);\nEntry? pair = bucket[index];\nif (pair == null) return null;\nreturn pair.val;\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\npublic void put(int key, String val)\n{\nEntry pair = new Entry(key, val);\nint index = hashFunc(key);\nbucket[index] = pair;\n}\n/* \u5220\u9664\u64cd\u4f5c */\npublic void remove(int key)\n{\nint index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nbucket[index] = null;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\npublic List<Entry> entrySet()\n{\nList<Entry> entrySet = new();\nforeach (Entry? pair in bucket)\n{\nif (pair != null)\nentrySet.Add(pair);\n}\nreturn entrySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\npublic List<int> keySet()\n{\nList<int> keySet = new();\nforeach (Entry? pair in bucket)\n{\nif (pair != null)\nkeySet.Add(pair.key);\n}\nreturn keySet;\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\npublic List<String> valueSet()\n{\nList<String> valueSet = new();\nforeach (Entry? pair in bucket)\n{\nif (pair != null)\nvalueSet.Add(pair.val);\n}\nreturn valueSet;\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\npublic void print()\n{\nforeach (Entry kv in entrySet())\n{\nConsole.WriteLine(kv.key + \" -> \" + kv.val);\n}\n}\n}\n</code></pre> array_hash_map.swift<pre><code>/* \u952e\u503c\u5bf9 int->String */\nclass Entry {\nvar key: Int\nvar val: String\ninit(key: Int, val: String) {\nself.key = key\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\nprivate var bucket: [Entry?] = []\ninit() {\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nfor _ in 0 ..< 100 {\nbucket.append(nil)\n}\n}\n/* \u54c8\u5e0c\u51fd\u6570 */\nprivate func hashFunc(key: Int) -> Int {\nlet index = key % 100\nreturn index\n}\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc get(key: Int) -> String? {\nlet index = hashFunc(key: key)\nlet pair = bucket[index]\nreturn pair?.val\n}\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc put(key: Int, val: String) {\nlet pair = Entry(key: key, val: val)\nlet index = hashFunc(key: key)\nbucket[index] = pair\n}\n/* \u5220\u9664\u64cd\u4f5c */\nfunc remove(key: Int) {\nlet index = hashFunc(key: key)\n// \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\nbucket[index] = nil\n}\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nfunc entrySet() -> [Entry] {\nvar entrySet: [Entry] = []\nfor pair in bucket {\nif let pair = pair {\nentrySet.append(pair)\n}\n}\nreturn entrySet\n}\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc keySet() -> [Int] {\nvar keySet: [Int] = []\nfor pair in bucket {\nif let pair = pair {\nkeySet.append(pair.key)\n}\n}\nreturn keySet\n}\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc valueSet() -> [String] {\nvar valueSet: [String] = []\nfor pair in bucket {\nif let pair = pair {\nvalueSet.append(pair.val)\n}\n}\nreturn valueSet\n}\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc print() {\nfor entry in entrySet() {\nSwift.print(\"\\(entry.key) -> \\(entry.val)\")\n}\n}\n}\n</code></pre> array_hash_map.zig<pre><code>// \u952e\u503c\u5bf9 int->String\nconst Entry = struct {\nkey: usize = undefined,\nval: []const u8 = undefined,\npub fn init(key: usize, val: []const u8) Entry {\nreturn Entry {\n.key = key,\n.val = val,\n};\n}\n};\n// \u57fa\u4e8e\u6570\u7ec4\u7b80\u6613\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\nreturn struct {\nbucket: ?std.ArrayList(?T) = null,\nmem_allocator: std.mem.Allocator = undefined,\nconst Self = @This();\n// \u6784\u9020\u65b9\u6cd5\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nself.mem_allocator = allocator;\n// \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\nself.bucket = std.ArrayList(?T).init(self.mem_allocator);\nvar i: i32 = 0;\nwhile (i < 100) : (i += 1) {\ntry self.bucket.?.append(null);\n}\n}\n// \u6790\u6784\u65b9\u6cd5\npub fn deinit(self: *Self) void {\nif (self.bucket != null) self.bucket.?.deinit();\n}\n// \u54c8\u5e0c\u51fd\u6570\nfn hashFunc(key: usize) usize {\nvar index = key % 100;\nreturn index;\n}\n// \u67e5\u8be2\u64cd\u4f5c\npub fn get(self: *Self, key: usize) []const u8 {\nvar index = hashFunc(key);\nvar pair = self.bucket.?.items[index];\nreturn pair.?.val;\n}\n// \u6dfb\u52a0\u64cd\u4f5c\npub fn put(self: *Self, key: usize, val: []const u8) !void {\nvar pair = Entry.init(key, val);\nvar index = hashFunc(key);\nself.bucket.?.items[index] = pair;\n}\n// \u5220\u9664\u64cd\u4f5c\npub fn remove(self: *Self, key: usize) !void {\nvar index = hashFunc(key);\n// \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\nself.bucket.?.items[index] = null;\n} // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\npub fn entrySet(self: *Self) !*std.ArrayList(T) {\nvar entry_set = std.ArrayList(T).init(self.mem_allocator);\nfor (self.bucket.?.items) |item| {\nif (item == null) continue;\ntry entry_set.append(item.?);\n}\nreturn &entry_set;\n} // \u83b7\u53d6\u6240\u6709\u952e\npub fn keySet(self: *Self) !*std.ArrayList(usize) {\nvar key_set = std.ArrayList(usize).init(self.mem_allocator);\nfor (self.bucket.?.items) |item| {\nif (item == null) continue;\ntry key_set.append(item.?.key);\n}\nreturn &key_set;\n} // \u83b7\u53d6\u6240\u6709\u503c\npub fn valueSet(self: *Self) !*std.ArrayList([]const u8) {\nvar value_set = std.ArrayList([]const u8).init(self.mem_allocator);\nfor (self.bucket.?.items) |item| {\nif (item == null) continue;\ntry value_set.append(item.?.val);\n}\nreturn &value_set;\n}\n// \u6253\u5370\u54c8\u5e0c\u8868\npub fn print(self: *Self) !void {\nvar entry_set = try self.entrySet();\ndefer entry_set.deinit();\nfor (entry_set.items) |item| {\nstd.debug.print(\"{} -> {s}\\n\", .{item.key, item.val});\n}\n}\n};\n}\n</code></pre>"},{"location":"chapter_hashing/hash_map/#614","title":"6.1.4. \u00a0 \u54c8\u5e0c\u51b2\u7a81","text":"<p>\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u4f1a\u53d1\u73b0\uff0c\u54c8\u5e0c\u51fd\u6570 \\(f(x) = x \\% 100\\) \u4f1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5931\u6548\u3002\u5177\u4f53\u5730\uff0c\u5f53\u8f93\u5165\u7684 key \u540e\u4e24\u4f4d\u76f8\u540c\u65f6\uff0c\u54c8\u5e0c\u51fd\u6570\u7684\u8ba1\u7b97\u7ed3\u679c\u4e5f\u76f8\u540c\uff0c\u6307\u5411\u540c\u4e00\u4e2a value \u3002\u4f8b\u5982\uff0c\u5206\u522b\u67e5\u8be2\u4e24\u4e2a\u5b66\u53f7 \\(12836\\) \u548c \\(20336\\) \uff0c\u5219\u6709</p> \\[ f(12836) = f(20336) = 36 \\] <p>\u4e24\u4e2a\u5b66\u53f7\u6307\u5411\u4e86\u540c\u4e00\u4e2a\u59d3\u540d\uff0c\u8fd9\u660e\u663e\u662f\u4e0d\u5bf9\u7684\uff0c\u6211\u4eec\u5c06\u8fd9\u79cd\u73b0\u8c61\u79f0\u4e3a\u300c\u54c8\u5e0c\u51b2\u7a81 Hash Collision\u300d\u3002\u5982\u4f55\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u7684\u95ee\u9898\u5c06\u88ab\u7559\u5728\u4e0b\u7ae0\u8ba8\u8bba\u3002</p> <p></p> <p> Fig. \u54c8\u5e0c\u51b2\u7a81\u793a\u4f8b </p> <p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4e00\u4e2a\u4f18\u79c0\u7684\u300c\u54c8\u5e0c\u51fd\u6570\u300d\u5e94\u8be5\u5177\u5907\u4ee5\u4e0b\u7279\u6027\uff1a</p> <ul> <li>\u5c3d\u91cf\u5c11\u5730\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\uff1b</li> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff0c\u8ba1\u7b97\u5c3d\u53ef\u80fd\u9ad8\u6548\uff1b</li> <li>\u7a7a\u95f4\u4f7f\u7528\u7387\u9ad8\uff0c\u5373\u201c\u952e\u503c\u5bf9\u5360\u7528\u7a7a\u95f4 / \u54c8\u5e0c\u8868\u603b\u5360\u7528\u7a7a\u95f4\u201d\u5c3d\u53ef\u80fd\u5927\uff1b</li> </ul>"},{"location":"chapter_hashing/summary/","title":"6.3. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u5411\u54c8\u5e0c\u8868\u4e2d\u8f93\u5165\u4e00\u4e2a\u952e key \uff0c\u67e5\u8be2\u5230\u503c value \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(1)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</li> <li>\u54c8\u5e0c\u8868\u7684\u5e38\u7528\u64cd\u4f5c\u5305\u62ec\u67e5\u8be2\u3001\u6dfb\u52a0\u4e0e\u5220\u9664\u952e\u503c\u5bf9\u3001\u904d\u5386\u952e\u503c\u5bf9\u7b49\u3002</li> <li>\u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u5230\u6876\uff08\u6570\u7ec4\uff09\u7d22\u5f15\uff0c\u4ece\u800c\u8bbf\u95ee\u5230\u5bf9\u5e94\u7684\u503c value \u3002</li> <li>\u4e24\u4e2a\u4e0d\u540c\u7684 key \u7ecf\u8fc7\u54c8\u5e0c\u51fd\u6570\u53ef\u80fd\u5f97\u5230\u76f8\u540c\u7684\u6876\u7d22\u5f15\uff0c\u8fdb\u800c\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\uff0c\u5bfc\u81f4\u67e5\u8be2\u9519\u8bef\u3002</li> <li>\u7f13\u89e3\u54c8\u5e0c\u51b2\u7a81\u7684\u9014\u5f84\u6709\u4e24\u79cd\uff1a\u54c8\u5e0c\u8868\u6269\u5bb9\u3001\u4f18\u5316\u54c8\u5e0c\u8868\u7684\u8868\u793a\u65b9\u5f0f\u3002</li> <li>\u8d1f\u8f7d\u56e0\u5b50\u5b9a\u4e49\u4e3a\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u69fd\u6570\u91cf\uff0c\u4f53\u73b0\u54c8\u5e0c\u51b2\u7a81\u7684\u4e25\u91cd\u7a0b\u5ea6\uff0c\u5e38\u7528\u4f5c\u54c8\u5e0c\u8868\u6269\u5bb9\u7684\u89e6\u53d1\u6761\u4ef6\u3002\u4e0e\u6570\u7ec4\u6269\u5bb9\u7684\u539f\u7406\u7c7b\u4f3c\uff0c\u54c8\u5e0c\u8868\u6269\u5bb9\u64cd\u4f5c\u5f00\u9500\u4e5f\u5f88\u5927\u3002</li> <li>\u94fe\u5f0f\u5730\u5740\u8003\u8651\u5c06\u5355\u4e2a\u5143\u7d20\u8f6c\u5316\u6210\u4e00\u4e2a\u94fe\u8868\uff0c\u5c06\u6240\u6709\u51b2\u7a81\u5143\u7d20\u90fd\u5b58\u50a8\u5728\u4e00\u4e2a\u94fe\u8868\u4e2d\uff0c\u4ece\u800c\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u800c\u4e3a\u4e86\u63d0\u5347\u67e5\u8be2\u6548\u7387\uff0c\u53ef\u4ee5\u628a\u94fe\u8868\u8f6c\u5316\u4e3a AVL \u6811\u6216\u7ea2\u9ed1\u6811\uff0c</li> <li>\u5f00\u653e\u5bfb\u5740\u901a\u8fc7\u591a\u6b21\u63a2\u6d4b\u6765\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u3002\u7ebf\u6027\u63a2\u6d4b\u4f7f\u7528\u56fa\u5b9a\u6b65\u957f\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u5220\u9664\u5143\u7d20\u4e14\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\u3002\u591a\u6b21\u54c8\u5e0c\u4f7f\u7528\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u8fdb\u884c\u63a2\u6d4b\uff0c\u76f8\u5bf9\u7ebf\u6027\u63a2\u6d4b\u4e0d\u5bb9\u6613\u4ea7\u751f\u805a\u96c6\uff0c\u4ee3\u4ef7\u662f\u591a\u4e2a\u54c8\u5e0c\u51fd\u6570\u589e\u52a0\u4e86\u8ba1\u7b97\u91cf\u3002</li> <li>\u5728\u5de5\u4e1a\u754c\u4e2d\uff0cJava \u7684 HashMap \u91c7\u7528\u94fe\u5f0f\u5730\u5740\u3001Python \u7684 Dict \u91c7\u7528\u5f00\u653e\u5bfb\u5740\u3002</li> </ul>"},{"location":"chapter_heap/build_heap/","title":"8.2. \u00a0 \u5efa\u5806\u64cd\u4f5c *","text":"<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u6839\u636e\u8f93\u5165\u5217\u8868\u6765\u751f\u6210\u4e00\u4e2a\u5806\uff0c\u8fd9\u6837\u7684\u64cd\u4f5c\u88ab\u79f0\u4e3a\u300c\u5efa\u5806\u300d\u3002</p>"},{"location":"chapter_heap/build_heap/#821","title":"8.2.1. \u00a0 \u4e24\u79cd\u5efa\u5806\u65b9\u6cd5","text":""},{"location":"chapter_heap/build_heap/#_1","title":"\u501f\u52a9\u5165\u5806\u65b9\u6cd5\u5b9e\u73b0","text":"<p>\u6700\u76f4\u63a5\u5730\uff0c\u8003\u8651\u501f\u52a9\u300c\u5143\u7d20\u5165\u5806\u300d\u65b9\u6cd5\uff0c\u5148\u5efa\u7acb\u4e00\u4e2a\u7a7a\u5806\uff0c\u518d\u5c06\u5217\u8868\u5143\u7d20\u4f9d\u6b21\u5165\u5806\u5373\u53ef\u3002</p>"},{"location":"chapter_heap/build_heap/#_2","title":"\u57fa\u4e8e\u5806\u5316\u64cd\u4f5c\u5b9e\u73b0","text":"<p>\u7136\u800c\uff0c\u5b58\u5728\u4e00\u79cd\u66f4\u52a0\u9ad8\u6548\u7684\u5efa\u5806\u65b9\u6cd5\u3002\u8bbe\u5143\u7d20\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6211\u4eec\u5148\u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\uff0c\u7136\u540e\u8fed\u4ee3\u5730\u5bf9\u5404\u4e2a\u7ed3\u70b9\u6267\u884c\u300c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u300d\u3002\u5f53\u7136\uff0c\u65e0\u9700\u5bf9\u53f6\u7ed3\u70b9\u6267\u884c\u5806\u5316\uff0c\u56e0\u4e3a\u5176\u6ca1\u6709\u5b50\u7ed3\u70b9\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig my_heap.java<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<Integer> nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = new ArrayList<>(nums);\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nfor (int i = parent(size() - 1); i >= 0; i--) {\nsiftDown(i);\n}\n}\n</code></pre> my_heap.cpp<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector<int> nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = nums;\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nfor (int i = parent(size() - 1); i >= 0; i--) {\nsiftDown(i);\n}\n}\n</code></pre> my_heap.py<pre><code>\"\"\" \u6784\u9020\u65b9\u6cd5 \"\"\"\ndef __init__(self, nums: List[int]):\n# \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nself.max_heap = nums\n# \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nfor i in range(self.parent(self.size() - 1), -1, -1):\nself.sift_down(i)\n</code></pre> my_heap.go<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nh := &maxHeap{data: nums}\nfor i := len(h.data) - 1; i >= 0; i-- {\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nh.siftDown(i)\n}\nreturn h\n}\n</code></pre> my_heap.js<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nthis.#maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nfor (let i = this.#parent(this.size() - 1); i >= 0; i--) {\nthis.#siftDown(i);\n}\n}\n</code></pre> my_heap.ts<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nthis.maxHeap = nums === undefined ? [] : [...nums];\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nfor (let i = this.parent(this.size() - 1); i >= 0; i--) {\nthis.siftDown(i);\n}\n}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{newMaxHeap}\n</code></pre> my_heap.cs<pre><code>/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable<int> nums)\n{\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = new List<int>(nums);\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nvar size = parent(this.size() - 1);\nfor (int i = size; i >= 0; i--)\n{\nsiftDown(i);\n}\n}\n</code></pre> my_heap.swift<pre><code>/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\nmaxHeap = nums\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nfor i in stride(from: parent(i: size() - 1), through: 0, by: -1) {\nsiftDown(i: i)\n}\n}\n</code></pre> my_heap.zig<pre><code>// \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\nfn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {\nif (self.maxHeap != null) return;\nself.maxHeap = std.ArrayList(T).init(allocator);\n// \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\ntry self.maxHeap.?.appendSlice(nums);\n// \u5806\u5316\u9664\u53f6\u7ed3\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u7ed3\u70b9\nvar i: usize = parent(self.size() - 1) + 1;\nwhile (i > 0) : (i -= 1) {\ntry self.siftDown(i - 1);\n}\n}\n</code></pre>"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u5bf9\u4e8e\u7b2c\u4e00\u79cd\u5efa\u5806\u65b9\u6cd5\uff0c\u5143\u7d20\u5165\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u800c\u5e73\u5747\u957f\u5ea6\u4e3a \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u8be5\u65b9\u6cd5\u7684\u603b\u4f53\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002</p> <p>\u90a3\u4e48\uff0c\u7b2c\u4e8c\u79cd\u5efa\u5806\u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u591a\u5c11\u5462\uff1f\u6211\u4eec\u6765\u5c55\u5f00\u63a8\u7b97\u4e00\u4e0b\u3002</p> <ul> <li>\u5b8c\u5168\u4e8c\u53c9\u6811\u4e2d\uff0c\u8bbe\u7ed3\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u53f6\u7ed3\u70b9\u6570\u91cf\u4e3a \\((n + 1) / 2\\) \uff0c\u5176\u4e2d \\(/\\) \u4e3a\u5411\u4e0b\u6574\u9664\u3002\u56e0\u6b64\u5728\u6392\u9664\u53f6\u7ed3\u70b9\u540e\uff0c\u9700\u8981\u5806\u5316\u7ed3\u70b9\u6570\u91cf\u4e3a \\((n - 1)/2\\) \uff0c\u5373\u4e3a \\(O(n)\\) \uff1b</li> <li>\u4ece\u9876\u81f3\u5e95\u5806\u5316\u4e2d\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u6700\u591a\u5806\u5316\u81f3\u53f6\u7ed3\u70b9\uff0c\u56e0\u6b64\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u4e3a\u4e8c\u53c9\u6811\u9ad8\u5ea6 \\(O(\\log n)\\) \uff1b</li> </ul> <p>\u5c06\u4e0a\u8ff0\u4e24\u8005\u76f8\u4e58\uff0c\u53ef\u5f97\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \u3002\u7136\u800c\uff0c\u8be5\u4f30\u7b97\u7ed3\u679c\u4ecd\u4e0d\u591f\u51c6\u786e\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8003\u8651\u5230 \u4e8c\u53c9\u6811\u5e95\u5c42\u7ed3\u70b9\u8fdc\u591a\u4e8e\u9876\u5c42\u7ed3\u70b9 \u7684\u6027\u8d28\u3002</p> <p>\u4e0b\u9762\u6211\u4eec\u6765\u5c1d\u8bd5\u5c55\u5f00\u8ba1\u7b97\u3002\u4e3a\u4e86\u51cf\u5c0f\u8ba1\u7b97\u96be\u5ea6\uff0c\u6211\u4eec\u5047\u8bbe\u6811\u662f\u4e00\u4e2a\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u300d\uff0c\u8be5\u5047\u8bbe\u4e0d\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002\u8bbe\u4e8c\u53c9\u6811\uff08\u5373\u5806\uff09\u7ed3\u70b9\u6570\u91cf\u4e3a \\(n\\) \uff0c\u6811\u9ad8\u5ea6\u4e3a \\(h\\) \u3002\u4e0a\u6587\u63d0\u5230\uff0c\u7ed3\u70b9\u5806\u5316\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u7b49\u4e8e\u8be5\u7ed3\u70b9\u5230\u53f6\u7ed3\u70b9\u7684\u8ddd\u79bb\uff0c\u800c\u8fd9\u6b63\u662f\u201c\u7ed3\u70b9\u9ad8\u5ea6\u201d\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5c06\u5404\u5c42\u7684\u201c\u7ed3\u70b9\u6570\u91cf \\(\\times\\) \u7ed3\u70b9\u9ad8\u5ea6\u201d\u6c42\u548c\uff0c\u5373\u53ef\u5f97\u5230\u6240\u6709\u7ed3\u70b9\u7684\u5806\u5316\u7684\u8fed\u4ee3\u6b21\u6570\u603b\u548c\u3002</p> \\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\cdots + 2^{(h-1)}\\times1 \\] <p></p> <p> Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5404\u5c42\u7ed3\u70b9\u6570\u91cf </p> <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\u6613\u5f97</p> \\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\cdots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\cdots + 2^{h}\\times1 \\newline \\end{aligned} \\] <p>\u4f7f\u7528\u9519\u4f4d\u76f8\u51cf\u6cd5\uff0c\u4ee4\u4e0b\u5f0f \\(2 T(h)\\) \u51cf\u53bb\u4e0a\u5f0f \\(T(h)\\) \uff0c\u53ef\u5f97</p> \\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\cdots + 2^{h-1} + 2^h \\] <p>\u89c2\u5bdf\u4e0a\u5f0f\uff0c\\(T(h)\\) \u662f\u4e00\u4e2a\u7b49\u6bd4\u6570\u5217\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u6c42\u548c\u516c\u5f0f\uff0c\u5f97\u5230\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a</p> \\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h \\newline & = O(2^h) \\end{aligned} \\] <p>\u8fdb\u4e00\u6b65\u5730\uff0c\u9ad8\u5ea6\u4e3a \\(h\\) \u7684\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u7ed3\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\uff08Heap\uff09","text":"<p>\u300c\u5806 Heap\u300d\u662f\u4e00\u68f5\u9650\u5b9a\u6761\u4ef6\u4e0b\u7684\u300c\u5b8c\u5168\u4e8c\u53c9\u6811\u300d\u3002\u6839\u636e\u6210\u7acb\u6761\u4ef6\uff0c\u5806\u4e3b\u8981\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a</p> <ul> <li>\u300c\u5927\u9876\u5806 Max Heap\u300d\uff0c\u4efb\u610f\u7ed3\u70b9\u7684\u503c \\(\\geq\\) \u5176\u5b50\u7ed3\u70b9\u7684\u503c\uff1b</li> <li>\u300c\u5c0f\u9876\u5806 Min Heap\u300d\uff0c\u4efb\u610f\u7ed3\u70b9\u7684\u503c \\(\\leq\\) \u5176\u5b50\u7ed3\u70b9\u7684\u503c\uff1b</li> </ul> <p></p> <p> Fig. \u5c0f\u9876\u5806\u4e0e\u5927\u9876\u5806 </p>"},{"location":"chapter_heap/heap/#811","title":"8.1.1. \u00a0 \u5806\u672f\u8bed\u4e0e\u6027\u8d28","text":"<ul> <li>\u7531\u4e8e\u5806\u662f\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u56e0\u6b64\u6700\u5e95\u5c42\u7ed3\u70b9\u9760\u5de6\u586b\u5145\uff0c\u5176\u5b83\u5c42\u7ed3\u70b9\u7686\u88ab\u586b\u6ee1\u3002</li> <li>\u4e8c\u53c9\u6811\u4e2d\u7684\u6839\u7ed3\u70b9\u5bf9\u5e94\u300c\u5806\u9876\u300d\uff0c\u5e95\u5c42\u6700\u9760\u53f3\u7ed3\u70b9\u5bf9\u5e94\u300c\u5806\u5e95\u300d\u3002</li> <li>\u5bf9\u4e8e\u5927\u9876\u5806 / \u5c0f\u9876\u5806\uff0c\u5176\u5806\u9876\u5143\u7d20\uff08\u5373\u6839\u7ed3\u70b9\uff09\u7684\u503c\u6700\u5927 / \u6700\u5c0f\u3002</li> </ul>"},{"location":"chapter_heap/heap/#812","title":"8.1.2. \u00a0 \u5806\u5e38\u7528\u64cd\u4f5c","text":"<p>\u503c\u5f97\u8bf4\u660e\u7684\u662f\uff0c\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u63d0\u4f9b\u7684\u662f\u300c\u4f18\u5148\u961f\u5217 Priority Queue\u300d\uff0c\u5176\u662f\u4e00\u79cd\u62bd\u8c61\u6570\u636e\u7ed3\u6784\uff0c\u5b9a\u4e49\u4e3a\u5177\u6709\u51fa\u961f\u4f18\u5148\u7ea7\u7684\u961f\u5217\u3002</p> <p>\u800c\u6070\u597d\uff0c\u5806\u7684\u5b9a\u4e49\u4e0e\u4f18\u5148\u961f\u5217\u7684\u64cd\u4f5c\u903b\u8f91\u5b8c\u5168\u543b\u5408\uff0c\u5927\u9876\u5806\u5c31\u662f\u4e00\u4e2a\u5143\u7d20\u4ece\u5927\u5230\u5c0f\u51fa\u961f\u7684\u4f18\u5148\u961f\u5217\u3002\u4ece\u4f7f\u7528\u89d2\u5ea6\u770b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u300c\u4f18\u5148\u961f\u5217\u300d\u548c\u300c\u5806\u300d\u7406\u89e3\u4e3a\u7b49\u4ef7\u7684\u6570\u636e\u7ed3\u6784\u3002\u56e0\u6b64\uff0c\u672c\u6587\u4e0e\u4ee3\u7801\u5bf9\u4e24\u8005\u4e0d\u505a\u7279\u522b\u533a\u5206\uff0c\u7edf\u4e00\u4f7f\u7528\u300c\u5806\u300d\u6765\u547d\u540d\u3002</p> <p>\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u89c1\u4e0b\u8868\uff08\u65b9\u6cd5\u547d\u540d\u4ee5 Java \u4e3a\u4f8b\uff09\u3002</p> \u65b9\u6cd5 \u63cf\u8ff0 \u65f6\u95f4\u590d\u6742\u5ea6 add() \u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) poll() \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>\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\u4e2d\u201c\u4ece\u5c0f\u5230\u5927\u6392\u5217\u201d\u548c\u201c\u4ece\u5927\u5230\u5c0f\u6392\u5217\u201d\uff0c\u201c\u5927\u9876\u5806\u201d\u548c\u201c\u5c0f\u9876\u5806\u201d\u53ef\u4ec5\u901a\u8fc7\u4fee\u6539 Comparator \u6765\u4e92\u76f8\u8f6c\u6362\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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.add(1);\nmaxHeap.add(3);\nmaxHeap.add(2);\nmaxHeap.add(5);\nmaxHeap.add(4);\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = heap.poll(); // 5\npeek = heap.poll(); // 4\npeek = heap.poll(); // 3\npeek = heap.poll(); // 2\npeek = heap.poll(); // 1\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n</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.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 = 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 = len(max_heap)\n\"\"\" \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a \"\"\"\nis_empty = not max_heap\n\"\"\" \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 \"\"\"\nmin_heap = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n</code></pre> heap.go<pre><code>// Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n// Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n// \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n*h = append(*h, x.(int))\n}\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n// \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\nlast := (*h)[len(*h)-1]\n*h = (*h)[:len(*h)-1]\nreturn last\n}\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\nreturn len(*h)\n}\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n// \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\nreturn (*h)[i].(int) > (*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)\nheap.Pop(maxHeap)\nheap.Pop(maxHeap)\nheap.Pop(maxHeap)\nheap.Pop(maxHeap)\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nsize := len(*maxHeap)\nfmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(*maxHeap) == 0\nfmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n</code></pre> heap.js<pre><code>// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e heap \u7c7b\n</code></pre> heap.ts<pre><code>// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e\u5806 Heap \u7c7b\n</code></pre> heap.c<pre><code>\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.swift<pre><code>// Swift \u672a\u63d0\u4f9b\u5185\u7f6e heap \u7c7b\n</code></pre> heap.zig<pre><code>\n</code></pre>"},{"location":"chapter_heap/heap/#813","title":"8.1.3. \u00a0 \u5806\u7684\u5b9e\u73b0","text":"<p>\u4e0b\u6587\u5b9e\u73b0\u7684\u662f\u300c\u5927\u9876\u5806\u300d\uff0c\u82e5\u60f3\u8f6c\u6362\u4e3a\u300c\u5c0f\u9876\u5806\u300d\uff0c\u5c06\u6240\u6709\u5927\u5c0f\u903b\u8f91\u5224\u65ad\u53d6\u9006\uff08\u4f8b\u5982\u5c06 \\(\\geq\\) \u66ff\u6362\u4e3a \\(\\leq\\) \uff09\u5373\u53ef\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u81ea\u884c\u5b9e\u73b0\u3002</p>"},{"location":"chapter_heap/heap/#_1","title":"\u5806\u7684\u5b58\u50a8\u4e0e\u8868\u793a","text":"<p>\u5728\u4e8c\u53c9\u6811\u7ae0\u8282\u6211\u4eec\u5b66\u8fc7\uff0c\u300c\u5b8c\u5168\u4e8c\u53c9\u6811\u300d\u975e\u5e38\u9002\u5408\u4f7f\u7528\u300c\u6570\u7ec4\u300d\u6765\u8868\u793a\uff0c\u800c\u5806\u6070\u597d\u662f\u4e00\u68f5\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u56e0\u800c\u6211\u4eec\u91c7\u7528\u300c\u6570\u7ec4\u300d\u6765\u5b58\u50a8\u300c\u5806\u300d\u3002</p> <p>\u4e8c\u53c9\u6811\u6307\u9488\u3002\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u4e8c\u53c9\u6811\u65f6\uff0c\u5143\u7d20\u4ee3\u8868\u7ed3\u70b9\u503c\uff0c\u7d22\u5f15\u4ee3\u8868\u7ed3\u70b9\u5728\u4e8c\u53c9\u6811\u4e2d\u7684\u4f4d\u7f6e\uff0c\u800c\u7ed3\u70b9\u6307\u9488\u901a\u8fc7\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u6765\u5b9e\u73b0\u3002</p> <p>\u5177\u4f53\u5730\uff0c\u7ed9\u5b9a\u7d22\u5f15 \\(i\\) \uff0c\u90a3\u4e48\u5176\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \u3001\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3001\u7236\u7ed3\u70b9\u7d22\u5f15\u4e3a \\((i - 1) / 2\\) \uff08\u5411\u4e0b\u6574\u9664\uff09\u3002\u5f53\u7d22\u5f15\u8d8a\u754c\u65f6\uff0c\u4ee3\u8868\u7a7a\u7ed3\u70b9\u6216\u7ed3\u70b9\u4e0d\u5b58\u5728\u3002</p> <p></p> <p> Fig. \u5806\u7684\u8868\u793a\u4e0e\u5b58\u50a8 </p> <p>\u6211\u4eec\u5c06\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig my_heap.java<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.cpp<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nint left(int i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nint right(int i) {\nreturn 2 * i + 2;\n} /* \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 */\nint parent(int i) {\nreturn (i - 1) / 2; // \u5411\u4e0b\u53d6\u6574\n}\n</code></pre> my_heap.py<pre><code>\"\"\" \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 \"\"\"\ndef left(self, i: int) -> int:\nreturn 2 * i + 1\n\"\"\" \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 \"\"\"\ndef right(self, i: int) -> int:\nreturn 2 * i + 2\n\"\"\" \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 \"\"\"\ndef parent(self, i: int) -> int:\nreturn (i - 1) // 2 # \u5411\u4e0b\u6574\u9664\n</code></pre> my_heap.go<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\nreturn 2*i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\nreturn 2*i + 2\n}\n/* \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n// \u5411\u4e0b\u6574\u9664\nreturn (i - 1) / 2\n}\n</code></pre> my_heap.js<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 */\n#left(i) {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 */\n#right(i) {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 */\n#parent(i) {\nreturn Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.ts<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nleft(i: number): number {\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nright(i: number): number {\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 */\nparent(i: number): number {\nreturn Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{left}\n[class]{maxHeap}-[func]{right}\n[class]{maxHeap}-[func]{parent}\n</code></pre> my_heap.cs<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nint left(int i)\n{\nreturn 2 * i + 1;\n}\n/* \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nint right(int i)\n{\nreturn 2 * i + 2;\n}\n/* \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 */\nint parent(int i)\n{\nreturn (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.swift<pre><code>/* \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n2 * i + 1\n}\n/* \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n2 * i + 2\n}\n/* \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n(i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n</code></pre> my_heap.zig<pre><code>// \u83b7\u53d6\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15\nfn left(i: usize) usize {\nreturn 2 * i + 1;\n}\n// \u83b7\u53d6\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15\nfn right(i: usize) usize {\nreturn 2 * i + 2;\n}\n// \u83b7\u53d6\u7236\u7ed3\u70b9\u7d22\u5f15\nfn parent(i: usize) usize {\n// return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\nreturn @divFloor(i - 1, 2);\n}\n</code></pre>"},{"location":"chapter_heap/heap/#_2","title":"\u8bbf\u95ee\u5806\u9876\u5143\u7d20","text":"<p>\u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u7684\u6839\u7ed3\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig my_heap.java<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap.get(0);\n}\n</code></pre> my_heap.cpp<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\nreturn maxHeap[0];\n}\n</code></pre> my_heap.py<pre><code>\"\"\" \u8bbf\u95ee\u5806\u9876\u5143\u7d20 \"\"\"\ndef peek(self) -> int:\nreturn self.max_heap[0]\n</code></pre> my_heap.go<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\nreturn h.data[0]\n}\n</code></pre> my_heap.js<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\nreturn this.#maxHeap[0];\n}\n</code></pre> my_heap.ts<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\nreturn this.maxHeap[0];\n}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{peek}\n</code></pre> my_heap.cs<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek()\n{\nreturn maxHeap[0];\n}\n</code></pre> my_heap.swift<pre><code>/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nmaxHeap[0]\n}\n</code></pre> my_heap.zig<pre><code>// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\nreturn self.maxHeap.?.items[0];\n} </code></pre>"},{"location":"chapter_heap/heap/#_3","title":"\u5143\u7d20\u5165\u5806","text":"<p>\u7ed9\u5b9a\u5143\u7d20 <code>val</code> \uff0c\u6211\u4eec\u5148\u5c06\u5176\u6dfb\u52a0\u5230\u5806\u5e95\u3002\u6dfb\u52a0\u540e\uff0c\u7531\u4e8e <code>val</code> \u53ef\u80fd\u5927\u4e8e\u5806\u4e2d\u5176\u5b83\u5143\u7d20\uff0c\u6b64\u65f6\u5806\u7684\u6210\u7acb\u6761\u4ef6\u53ef\u80fd\u5df2\u7ecf\u88ab\u7834\u574f\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u590d\u4ece\u63d2\u5165\u7ed3\u70b9\u5230\u6839\u7ed3\u70b9\u8fd9\u6761\u8def\u5f84\u4e0a\u7684\u5404\u4e2a\u7ed3\u70b9\uff0c\u8be5\u64cd\u4f5c\u88ab\u79f0\u4e3a\u300c\u5806\u5316 Heapify\u300d\u3002</p> <p>\u8003\u8651\u4ece\u5165\u5806\u7ed3\u70b9\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u6267\u884c\u5806\u5316\u3002\u5177\u4f53\u5730\uff0c\u6bd4\u8f83\u63d2\u5165\u7ed3\u70b9\u4e0e\u5176\u7236\u7ed3\u70b9\u7684\u503c\uff0c\u82e5\u63d2\u5165\u7ed3\u70b9\u66f4\u5927\u5219\u5c06\u5b83\u4eec\u4ea4\u6362\uff1b\u5e76\u5faa\u73af\u4ee5\u4e0a\u64cd\u4f5c\uff0c\u4ece\u5e95\u81f3\u9876\u5730\u4fee\u590d\u5806\u4e2d\u7684\u5404\u4e2a\u7ed3\u70b9\uff1b\u76f4\u81f3\u8d8a\u8fc7\u6839\u7ed3\u70b9\u65f6\u7ed3\u675f\uff0c\u6216\u5f53\u9047\u5230\u65e0\u9700\u4ea4\u6362\u7684\u7ed3\u70b9\u65f6\u63d0\u524d\u7ed3\u675f\u3002</p> <1><2><3><4><5><6> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p>\u8bbe\u7ed3\u70b9\u603b\u6570\u4e3a \\(n\\) \uff0c\u5219\u6811\u7684\u9ad8\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u6613\u5f97\u5806\u5316\u64cd\u4f5c\u7684\u5faa\u73af\u8f6e\u6570\u6700\u591a\u4e3a \\(O(\\log n)\\) \uff0c\u56e0\u800c\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig my_heap.java<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u7ed3\u70b9\nmaxHeap.add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\nbreak;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.cpp<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n// \u6dfb\u52a0\u7ed3\u70b9\nmaxHeap.push_back(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\nwhile (true) {\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\nint p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap[i] <= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nswap(maxHeap[i], maxHeap[p]);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.py<pre><code>\"\"\" \u5143\u7d20\u5165\u5806 \"\"\"\ndef push(self, val: int):\n# \u6dfb\u52a0\u7ed3\u70b9\nself.max_heap.append(val)\n# \u4ece\u5e95\u81f3\u9876\u5806\u5316\nself.sift_up(self.size() - 1)\n\"\"\" \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 \"\"\"\ndef sift_up(self, i: int):\nwhile True:\n# \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\np = self.parent(i)\n# \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 or self.max_heap[i] <= self.max_heap[p]:\nbreak\n# \u4ea4\u6362\u4e24\u7ed3\u70b9\nself.swap(i, p)\n# \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n</code></pre> my_heap.go<pre><code>/* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n// \u6dfb\u52a0\u7ed3\u70b9\nh.data = append(h.data, val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nh.siftUp(len(h.data) - 1)\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\nfor true {\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\np := h.parent(i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 || h.data[i].(int) <= h.data[p].(int) {\nbreak\n}\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nh.swap(i, p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n</code></pre> my_heap.js<pre><code>/* \u5143\u7d20\u5165\u5806 */\npush(val) {\n// \u6dfb\u52a0\u7ed3\u70b9\nthis.#maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.#siftUp(this.size() - 1);\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\nwhile (true) {\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\nconst p = this.#parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nthis.#swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.ts<pre><code>/* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n// \u6dfb\u52a0\u7ed3\u70b9\nthis.maxHeap.push(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nthis.siftUp(this.size() - 1);\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\nwhile (true) {\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\nconst p = this.parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nthis.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{push}\n[class]{maxHeap}-[func]{siftUp}\n</code></pre> my_heap.cs<pre><code>/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val)\n{\n// \u6dfb\u52a0\u7ed3\u70b9\nmaxHeap.Add(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(size() - 1);\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i)\n{\nwhile (true)\n{\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\nint p = parent(i);\n// \u82e5\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (p < 0 || maxHeap[i] <= maxHeap[p])\nbreak;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nswap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre> my_heap.swift<pre><code>/* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n// \u6dfb\u52a0\u7ed3\u70b9\nmaxHeap.append(val)\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\nsiftUp(i: size() - 1)\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\nvar i = i\nwhile true {\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\nlet p = parent(i: i)\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif p < 0 || maxHeap[i] <= maxHeap[p] {\nbreak\n}\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nswap(i: i, j: p)\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p\n}\n}\n</code></pre> my_heap.zig<pre><code>// \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n// \u6dfb\u52a0\u7ed3\u70b9\ntry self.maxHeap.?.append(val);\n// \u4ece\u5e95\u81f3\u9876\u5806\u5316\ntry self.siftUp(self.size() - 1);\n} // \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u83b7\u53d6\u7ed3\u70b9 i \u7684\u7236\u7ed3\u70b9\nvar p = parent(i);\n// \u5f53\u201c\u8d8a\u8fc7\u6839\u7ed3\u70b9\u201d\u6216\u201c\u7ed3\u70b9\u65e0\u9700\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\nif (p < 0 or self.maxHeap.?.items[i] <= self.maxHeap.?.items[p]) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\ntry self.swap(i, p);\n// \u5faa\u73af\u5411\u4e0a\u5806\u5316\ni = p;\n}\n}\n</code></pre>"},{"location":"chapter_heap/heap/#_4","title":"\u5806\u9876\u5143\u7d20\u51fa\u5806","text":"<p>\u5806\u9876\u5143\u7d20\u662f\u4e8c\u53c9\u6811\u6839\u7ed3\u70b9\uff0c\u5373\u5217\u8868\u9996\u5143\u7d20\uff0c\u5982\u679c\u6211\u4eec\u76f4\u63a5\u5c06\u9996\u5143\u7d20\u4ece\u5217\u8868\u4e2d\u5220\u9664\uff0c\u5219\u4e8c\u53c9\u6811\u4e2d\u6240\u6709\u7ed3\u70b9\u90fd\u4f1a\u968f\u4e4b\u53d1\u751f\u79fb\u4f4d\uff08\u7d22\u5f15\u53d1\u751f\u53d8\u5316\uff09\uff0c\u8fd9\u6837\u540e\u7eed\u4f7f\u7528\u5806\u5316\u4fee\u590d\u5c31\u5f88\u9ebb\u70e6\u4e86\u3002\u4e3a\u4e86\u5c3d\u91cf\u51cf\u5c11\u5143\u7d20\u7d22\u5f15\u53d8\u52a8\uff0c\u91c7\u53d6\u4ee5\u4e0b\u64cd\u4f5c\u6b65\u9aa4\uff1a</p> <ol> <li>\u4ea4\u6362\u5806\u9876\u5143\u7d20\u4e0e\u5806\u5e95\u5143\u7d20\uff08\u5373\u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff09\uff1b</li> <li>\u4ea4\u6362\u5b8c\u6210\u540e\uff0c\u5c06\u5806\u5e95\u4ece\u5217\u8868\u4e2d\u5220\u9664\uff08\u6ce8\u610f\uff0c\u56e0\u4e3a\u5df2\u7ecf\u4ea4\u6362\uff0c\u5b9e\u9645\u4e0a\u5220\u9664\u7684\u662f\u539f\u6765\u7684\u5806\u9876\u5143\u7d20\uff09\uff1b</li> <li>\u4ece\u6839\u7ed3\u70b9\u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u6267\u884c\u5806\u5316\uff1b</li> </ol> <p>\u987e\u540d\u601d\u4e49\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\u7684\u64cd\u4f5c\u65b9\u5411\u4e0e\u4ece\u5e95\u81f3\u9876\u5806\u5316\u76f8\u53cd\uff0c\u6211\u4eec\u6bd4\u8f83\u6839\u7ed3\u70b9\u7684\u503c\u4e0e\u5176\u4e24\u4e2a\u5b50\u7ed3\u70b9\u7684\u503c\uff0c\u5c06\u6700\u5927\u7684\u5b50\u7ed3\u70b9\u4e0e\u6839\u7ed3\u70b9\u6267\u884c\u4ea4\u6362\uff0c\u5e76\u5faa\u73af\u4ee5\u4e0a\u64cd\u4f5c\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u65f6\u7ed3\u675f\uff0c\u6216\u5f53\u9047\u5230\u65e0\u9700\u4ea4\u6362\u7684\u7ed3\u70b9\u65f6\u63d0\u524d\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>\u4e0e\u5143\u7d20\u5165\u5806\u64cd\u4f5c\u7c7b\u4f3c\uff0c\u5806\u9876\u5143\u7d20\u51fa\u5806\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig my_heap.java<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nint poll() {\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new EmptyStackException();\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u7ed3\u70b9\nint val = maxHeap.remove(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l < size() && maxHeap.get(l) > maxHeap.get(ma))\nma = l;\nif (r < size() && maxHeap.get(r) > maxHeap.get(ma))\nma = r;\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.cpp<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nvoid poll() {\n// \u5224\u7a7a\u5904\u7406\nif (empty()) {\nthrow out_of_range(\"\u5806\u4e3a\u7a7a\");\n}\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(maxHeap[0], maxHeap[size() - 1]);\n// \u5220\u9664\u7ed3\u70b9\nmaxHeap.pop_back();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\nwhile (true) {\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (l < size() && maxHeap[l] > maxHeap[ma]) ma = l;\nif (r < size() && maxHeap[r] > maxHeap[ma])\nma = r;\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\nswap(maxHeap[i], maxHeap[ma]);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.py<pre><code>\"\"\" \u5143\u7d20\u51fa\u5806 \"\"\"\ndef poll(self) -> int:\n# \u5224\u7a7a\u5904\u7406\nassert not self.is_empty()\n# \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nself.swap(0, self.size() - 1)\n# \u5220\u9664\u7ed3\u70b9\nval = self.max_heap.pop()\n# \u4ece\u9876\u81f3\u5e95\u5806\u5316\nself.sift_down(0)\n# \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n\"\"\" \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 \"\"\"\ndef sift_down(self, i: int):\nwhile True:\n# \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nl, r, ma = self.left(i), self.right(i), i\nif l < self.size() and self.max_heap[l] > self.max_heap[ma]:\nma = l\nif r < self.size() and self.max_heap[r] > self.max_heap[ma]:\nma = r\n# \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i:\nbreak\n# \u4ea4\u6362\u4e24\u7ed3\u70b9\nself.swap(i, ma)\n# \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n</code></pre> my_heap.go<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) poll() any {\n// \u5224\u7a7a\u5904\u7406\nif h.isEmpty() {\nfmt.Println(\"error\")\nreturn nil\n}\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nh.swap(0, h.size()-1)\n// \u5220\u9664\u7ed3\u70b9\nval := h.data[len(h.data)-1]\nh.data = h.data[:len(h.data)-1]\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nh.siftDown(0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\nfor true {\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a max\nl, r, max := h.left(i), h.right(i), i\nif l < h.size() && h.data[l].(int) > h.data[max].(int) {\nmax = l\n}\nif r < h.size() && h.data[r].(int) > h.data[max].(int) {\nmax = r\n}\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif max == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nh.swap(i, max)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = max\n}\n}\n</code></pre> my_heap.js<pre><code>/* \u5143\u7d20\u51fa\u5806 */\npoll() {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new Error(\"\u5806\u4e3a\u7a7a\");\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.#swap(0, this.size() - 1);\n// \u5220\u9664\u7ed3\u70b9\nconst val = this.#maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.#siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\nwhile (true) {\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.#left(i),\nr = this.#right(i);\nlet ma = i;\nif (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\nif (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nthis.#swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.ts<pre><code>/* \u5143\u7d20\u51fa\u5806 */\npoll(): number {\n// \u5224\u7a7a\u5904\u7406\nif (this.isEmpty()) throw new RangeError(\"Heap is empty.\");\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nthis.swap(0, this.size() - 1);\n// \u5220\u9664\u7ed3\u70b9\nconst val = this.maxHeap.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nthis.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\nwhile (true) {\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nconst l = this.left(i), r = this.right(i);\nlet ma = i;\nif (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\nif (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nthis.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.c<pre><code>[class]{maxHeap}-[func]{poll}\n[class]{maxHeap}-[func]{siftDown}\n</code></pre> my_heap.cs<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nint poll()\n{\n// \u5224\u7a7a\u5904\u7406\nif (isEmpty())\nthrow new IndexOutOfRangeException();\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(0, size() - 1);\n// \u5220\u9664\u7ed3\u70b9\nint val = maxHeap.Last();\nmaxHeap.RemoveAt(size() - 1);\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i)\n{\nwhile (true)\n{\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nint l = left(i), r = right(i), ma = i;\nif (l < size() && maxHeap[l] > maxHeap[ma])\nma = l;\nif (r < size() && maxHeap[r] > maxHeap[ma])\nma = r;\n// \u82e5\u201c\u7ed3\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nswap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre> my_heap.swift<pre><code>/* \u5143\u7d20\u51fa\u5806 */\nfunc poll() -> Int {\n// \u5224\u7a7a\u5904\u7406\nif isEmpty() {\nfatalError(\"\u5806\u4e3a\u7a7a\")\n}\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\nswap(i: 0, j: size() - 1)\n// \u5220\u9664\u7ed3\u70b9\nlet val = maxHeap.remove(at: size() - 1)\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\nsiftDown(i: 0)\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val\n}\n/* \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\nvar i = i\nwhile true {\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nlet l = left(i: i)\nlet r = right(i: i)\nvar ma = i\nif l < size(), maxHeap[l] > maxHeap[ma] {\nma = l\n}\nif r < size(), maxHeap[r] > maxHeap[ma] {\nma = r\n}\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif ma == i {\nbreak\n}\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\nswap(i: i, j: ma)\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma\n}\n}\n</code></pre> my_heap.zig<pre><code>// \u5143\u7d20\u51fa\u5806\nfn poll(self: *Self) !T {\n// \u5224\u65ad\u5904\u7406\nif (self.isEmpty()) unreachable;\n// \u4ea4\u6362\u6839\u7ed3\u70b9\u4e0e\u6700\u53f3\u53f6\u7ed3\u70b9\uff08\u5373\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\ntry self.swap(0, self.size() - 1);\n// \u5220\u9664\u7ed3\u70b9\nvar val = self.maxHeap.?.pop();\n// \u4ece\u9876\u81f3\u5e95\u5806\u5316\ntry self.siftDown(0);\n// \u8fd4\u56de\u5806\u9876\u5143\u7d20\nreturn val;\n} // \u4ece\u7ed3\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\nvar i = i_;\nwhile (true) {\n// \u5224\u65ad\u7ed3\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u7ed3\u70b9\uff0c\u8bb0\u4e3a ma\nvar l = left(i);\nvar r = right(i);\nvar ma = i;\nif (l < self.size() and self.maxHeap.?.items[l] > self.maxHeap.?.items[ma]) ma = l;\nif (r < self.size() and self.maxHeap.?.items[r] > self.maxHeap.?.items[ma]) ma = r;\n// \u82e5\u7ed3\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u9700\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\nif (ma == i) break;\n// \u4ea4\u6362\u4e24\u7ed3\u70b9\ntry self.swap(i, ma);\n// \u5faa\u73af\u5411\u4e0b\u5806\u5316\ni = ma;\n}\n}\n</code></pre>"},{"location":"chapter_heap/heap/#814","title":"8.1.4. \u00a0 \u5806\u5e38\u89c1\u5e94\u7528","text":"<ul> <li>\u4f18\u5148\u961f\u5217\u3002\u5806\u5e38\u4f5c\u4e3a\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u7684\u9996\u9009\u6570\u636e\u7ed3\u6784\uff0c\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(\\log n)\\) \uff0c\u5efa\u961f\u64cd\u4f5c\u4e3a \\(O(n)\\) \uff0c\u7686\u975e\u5e38\u9ad8\u6548\u3002</li> <li>\u5806\u6392\u5e8f\u3002\u7ed9\u5b9a\u4e00\u7ec4\u6570\u636e\uff0c\u6211\u4eec\u4f7f\u7528\u5176\u5efa\u5806\uff0c\u5e76\u4f9d\u6b21\u5168\u90e8\u5f39\u51fa\uff0c\u5219\u53ef\u4ee5\u5f97\u5230\u6709\u5e8f\u7684\u5e8f\u5217\u3002\u5f53\u7136\uff0c\u5806\u6392\u5e8f\u4e00\u822c\u65e0\u9700\u5f39\u51fa\u5143\u7d20\uff0c\u4ec5\u9700\u6bcf\u8f6e\u5c06\u5806\u9876\u5143\u7d20\u4ea4\u6362\u81f3\u6570\u7ec4\u5c3e\u90e8\u5e76\u51cf\u5c0f\u5806\u7684\u957f\u5ea6\u5373\u53ef\u3002</li> <li>\u83b7\u53d6\u6700\u5927\u7684 \\(k\\) \u4e2a\u5143\u7d20\u3002\u8fd9\u65e2\u662f\u4e00\u9053\u7ecf\u5178\u7b97\u6cd5\u9898\u76ee\uff0c\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u5e94\u7528\uff0c\u4f8b\u5982\u9009\u53d6\u70ed\u5ea6\u524d 10 \u7684\u65b0\u95fb\u4f5c\u4e3a\u5fae\u535a\u70ed\u641c\uff0c\u9009\u53d6\u524d 10 \u9500\u91cf\u7684\u5546\u54c1\u7b49\u3002</li> </ul>"},{"location":"chapter_heap/summary/","title":"8.3. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u5806\u662f\u4e00\u68f5\u9650\u5b9a\u6761\u4ef6\u4e0b\u7684\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\u6700\u5927\uff08\u5c0f\uff09\u3002</li> <li>\u4f18\u5148\u961f\u5217\u5b9a\u4e49\u4e3a\u4e00\u79cd\u5177\u6709\u51fa\u961f\u4f18\u5148\u7ea7\u7684\u961f\u5217\u3002\u5806\u662f\u5b9e\u73b0\u4f18\u5148\u961f\u5217\u7684\u6700\u5e38\u7528\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u5806\u7684\u5e38\u7528\u64cd\u4f5c\u548c\u5bf9\u5e94\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u5143\u7d20\u5165\u5806 \\(O(\\log n)\\) \u3001\u5806\u9876\u5143\u7d20\u51fa\u5806 \\(O(\\log n)\\) \u3001\u8bbf\u95ee\u5806\u9876\u5143\u7d20 \\(O(1)\\) \u7b49\u3002</li> <li>\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u6765\u8868\u793a\uff0c\u56e0\u6b64\u6211\u4eec\u4e00\u822c\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u5806\u3002</li> <li>\u5806\u5316\u64cd\u4f5c\u7528\u4e8e\u4fee\u590d\u5806\u7684\u7279\u6027\uff0c\u5728\u5165\u5806\u548c\u51fa\u5806\u64cd\u4f5c\u4e2d\u90fd\u4f1a\u4f7f\u7528\u5230\u3002</li> <li>\u8f93\u5165 \\(n\\) \u4e2a\u5143\u7d20\u5e76\u5efa\u5806\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u88ab\u4f18\u5316\u81f3 \\(O(n)\\) \uff0c\u975e\u5e38\u9ad8\u6548\u3002</li> </ul>"},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1. \u00a0 \u7b97\u6cd5\u65e0\u5904\u4e0d\u5728","text":"<p>\u542c\u5230\u201c\u7b97\u6cd5\u201d\u8fd9\u4e2a\u8bcd\uff0c\u6211\u4eec\u4e00\u822c\u4f1a\u8054\u60f3\u5230\u6570\u5b66\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u5927\u591a\u6570\u7b97\u6cd5\u5e76\u4e0d\u5305\u542b\u590d\u6742\u7684\u6570\u5b66\uff0c\u800c\u66f4\u50cf\u662f\u5728\u8003\u5bdf\u57fa\u672c\u903b\u8f91\uff0c\u800c\u8fd9\u4e9b\u903b\u8f91\u5728\u6211\u4eec\u65e5\u5e38\u751f\u6d3b\u4e2d\u5904\u5904\u53ef\u89c1\u3002</p> <p>\u5728\u6b63\u5f0f\u4ecb\u7ecd\u7b97\u6cd5\u4e4b\u524d\uff0c\u6211\u60f3\u544a\u8bc9\u4f60\u4e00\u4ef6\u6709\u8da3\u7684\u4e8b\uff1a\u5176\u5b9e\uff0c\u4f60\u5728\u8fc7\u53bb\u5df2\u7ecf\u5b66\u4f1a\u4e86\u5f88\u591a\u7b97\u6cd5\uff0c\u5e76\u4e14\u5df2\u7ecf\u4e60\u60ef\u5c06\u5b83\u4eec\u5e94\u7528\u5230\u65e5\u5e38\u751f\u6d3b\u4e2d\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u4ecb\u7ecd\u4e24\u4e2a\u5177\u4f53\u4f8b\u5b50\u6765\u4f50\u8bc1\u3002</p> <p>\u4f8b\u4e00\uff1a\u62fc\u79ef\u6728\u3002\u4e00\u5957\u79ef\u6728\uff0c\u9664\u4e86\u6709\u8bb8\u591a\u90e8\u4ef6\u4e4b\u5916\uff0c\u8fd8\u4f1a\u9644\u9001\u8be6\u7ec6\u7684\u62fc\u88c5\u8bf4\u660e\u4e66\u3002\u6211\u4eec\u6309\u7167\u8bf4\u660e\u4e66\u4e0a\u4e00\u6b65\u6b65\u64cd\u4f5c\uff0c\u5373\u53ef\u62fc\u51fa\u590d\u6742\u7684\u79ef\u6728\u6a21\u578b\u3002</p> <p>\u5982\u679c\u4ece\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u89d2\u5ea6\u770b\uff0c\u5927\u5927\u5c0f\u5c0f\u7684\u300c\u79ef\u6728\u300d\u5c31\u662f\u6570\u636e\u7ed3\u6784\uff0c\u800c\u300c\u62fc\u88c5\u8bf4\u660e\u4e66\u300d\u4e0a\u7684\u4e00\u7cfb\u5217\u6b65\u9aa4\u5c31\u662f\u7b97\u6cd5\u3002</p> <p>\u4f8b\u4e8c\uff1a\u67e5\u5b57\u5178\u3002\u5728\u5b57\u5178\u4e2d\uff0c\u6bcf\u4e2a\u6c49\u5b57\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u62fc\u97f3\uff0c\u800c\u5b57\u5178\u662f\u6309\u7167\u62fc\u97f3\u7684\u82f1\u6587\u5b57\u6bcd\u8868\u987a\u5e8f\u6392\u5217\u7684\u3002\u5047\u8bbe\u9700\u8981\u5728\u5b57\u5178\u4e2d\u67e5\u8be2\u4efb\u610f\u4e00\u4e2a\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u5b57\uff0c\u4e00\u822c\u6211\u4eec\u4f1a\u8fd9\u6837\u505a\uff1a</p> <ol> <li>\u6253\u5f00\u5b57\u5178\u5927\u81f4\u4e00\u534a\u9875\u6570\u7684\u4f4d\u7f6e\uff0c\u67e5\u770b\u6b64\u9875\u7684\u9996\u5b57\u6bcd\u662f\u4ec0\u4e48\uff08\u5047\u8bbe\u4e3a \\(m\\) \uff09\uff1b</li> <li>\u7531\u4e8e\u5728\u82f1\u6587\u5b57\u6bcd\u8868\u4e2d \\(r\\) \u5728 \\(m\\) \u7684\u540e\u9762\uff0c\u56e0\u6b64\u5e94\u6392\u9664\u5b57\u5178\u524d\u534a\u90e8\u5206\uff0c\u67e5\u627e\u8303\u56f4\u4ec5\u5269\u540e\u534a\u90e8\u5206\uff1b</li> <li>\u5faa\u73af\u6267\u884c\u6b65\u9aa4 1-2 \uff0c\u76f4\u5230\u627e\u5230\u62fc\u97f3\u9996\u5b57\u6bcd\u4e3a \\(r\\) \u7684\u9875\u7801\u65f6\u7ec8\u6b62\u3002</li> </ol> <1><2><3><4><5> <p></p> <p></p> <p></p> <p></p> <p></p> <p>\u67e5\u5b57\u5178\u8fd9\u4e2a\u5c0f\u5b66\u751f\u7684\u6807\u914d\u6280\u80fd\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u5927\u540d\u9f0e\u9f0e\u7684\u300c\u4e8c\u5206\u67e5\u627e\u300d\u3002\u4ece\u6570\u636e\u7ed3\u6784\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u5178\u770b\u4f5c\u662f\u4e00\u4e2a\u5df2\u6392\u5e8f\u7684\u300c\u6570\u7ec4\u300d\uff1b\u800c\u4ece\u7b97\u6cd5\u89d2\u5ea6\uff0c\u6211\u4eec\u53ef\u5c06\u4e0a\u8ff0\u67e5\u5b57\u5178\u7684\u4e00\u7cfb\u5217\u6307\u4ee4\u770b\u4f5c\u662f\u300c\u4e8c\u5206\u67e5\u627e\u300d\u7b97\u6cd5\u3002</p> <p>\u5c0f\u5230\u70f9\u996a\u4e00\u9053\u83dc\u3001\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\uff0c\u4f7f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u5c06\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u7f16\u5199\u4ee3\u7801\u6765\u8c03\u7528 CPU, GPU \u6267\u884c\u7b97\u6cd5\uff0c\u4ece\u800c\u5c06\u751f\u6d3b\u4e2d\u7684\u95ee\u9898\u642c\u8fd0\u5230\u8ba1\u7b97\u673a\u4e2d\uff0c\u66f4\u52a0\u9ad8\u6548\u5730\u89e3\u51b3\u5404\u5f0f\u5404\u6837\u7684\u590d\u6742\u95ee\u9898\u3002</p> <p>Tip</p> <p>\u8bfb\u5230\u8fd9\u91cc\uff0c\u5982\u679c\u4f60\u611f\u5230\u5bf9\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u3001\u6570\u7ec4\u3001\u4e8c\u5206\u67e5\u627e\u7b49\u6b64\u7c7b\u6982\u5ff5\u4e00\u77e5\u534a\u89e3\uff0c\u90a3\u4e48\u5c31\u592a\u597d\u4e86\uff01\u56e0\u4e3a\u8fd9\u6b63\u662f\u672c\u4e66\u5b58\u5728\u7684\u4ef7\u503c\uff0c\u63a5\u4e0b\u6765\uff0c\u672c\u4e66\u5c06\u4f1a\u4e00\u6b65\u6b65\u5730\u5f15\u5bfc\u4f60\u8fdb\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\u751f\u6d3b\u4e2d\u968f\u5904\u53ef\u89c1\uff0c\u5e76\u4e0d\u9ad8\u6df1\u83ab\u6d4b\u3002\u6211\u4eec\u5df2\u7ecf\u4e0d\u77e5\u4e0d\u89c9\u5730\u5b66\u4e60\u5230\u8bb8\u591a\u201c\u7b97\u6cd5\u201d\uff0c\u7528\u4e8e\u89e3\u51b3\u751f\u6d3b\u4e2d\u5927\u5927\u5c0f\u5c0f\u7684\u95ee\u9898\u3002</li> <li>\u201c\u67e5\u5b57\u5178\u201d\u7684\u539f\u7406\u548c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u4e00\u81f4\u3002\u4e8c\u5206\u4f53\u73b0\u5206\u800c\u6cbb\u4e4b\u7684\u91cd\u8981\u7b97\u6cd5\u601d\u60f3\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\u6570\u636e\u7ed3\u6784\u662f\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002</li> <li>\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e24\u8005\u7d27\u5bc6\u8054\u7cfb\u3002\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u5e95\u5ea7\uff0c\u7b97\u6cd5\u662f\u53d1\u6325\u6570\u636e\u7ed3\u6784\u7684\u821e\u53f0\u3002</li> <li>\u4e50\u9ad8\u79ef\u6728\u5bf9\u5e94\u6570\u636e\uff0c\u79ef\u6728\u5f62\u72b6\u548c\u8fde\u63a5\u5f62\u5f0f\u5bf9\u5e94\u6570\u636e\u7ed3\u6784\uff0c\u62fc\u88c5\u79ef\u6728\u7684\u6d41\u7a0b\u6b65\u9aa4\u5bf9\u5e94\u7b97\u6cd5\u3002</li> </ul>"},{"location":"chapter_introduction/what_is_dsa/","title":"1.2. \u00a0 \u7b97\u6cd5\u662f\u4ec0\u4e48","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1. \u00a0 \u7b97\u6cd5\u5b9a\u4e49","text":"<p>\u300c\u7b97\u6cd5 Algorithm\u300d\u662f\u5728\u6709\u9650\u65f6\u95f4\u5185\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u7684\u4e00\u7ec4\u6307\u4ee4\u6216\u64cd\u4f5c\u6b65\u9aa4\u3002\u7b97\u6cd5\u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a</p> <ul> <li>\u95ee\u9898\u662f\u660e\u786e\u7684\uff0c\u9700\u8981\u62e5\u6709\u660e\u786e\u7684\u8f93\u5165\u548c\u8f93\u51fa\u5b9a\u4e49\u3002</li> <li>\u89e3\u5177\u6709\u786e\u5b9a\u6027\uff0c\u5373\u7ed9\u5b9a\u76f8\u540c\u8f93\u5165\u65f6\uff0c\u8f93\u51fa\u4e00\u5b9a\u76f8\u540c\u3002</li> <li>\u5177\u6709\u53ef\u884c\u6027\uff0c\u53ef\u5728\u6709\u9650\u6b65\u9aa4\u3001\u6709\u9650\u65f6\u95f4\u3001\u6709\u9650\u5185\u5b58\u7a7a\u95f4\u4e0b\u5b8c\u6210\u3002</li> <li>\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\uff0c\u5373\u53ef\u7528\u591a\u79cd\u8bed\u8a00\u5b9e\u73b0\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\u5728\u8ba1\u7b97\u673a\u4e2d\u7ec4\u7ec7\u4e0e\u5b58\u50a8\u6570\u636e\u7684\u65b9\u5f0f\u3002\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u5b58\u50a8\u548c\u64cd\u4f5c\u6027\u80fd\uff0c\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u539f\u5219\u6709\uff1a</p> <ul> <li>\u7a7a\u95f4\u5360\u7528\u5c3d\u53ef\u80fd\u5c0f\uff0c\u8282\u7701\u8ba1\u7b97\u673a\u5185\u5b58\u3002</li> <li>\u6570\u636e\u64cd\u4f5c\u5c3d\u91cf\u5feb\uff0c\u5305\u62ec\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\u7b97\u6cd5\u9ad8\u6548\u8fd0\u884c\u3002</li> </ul> <p>\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u5145\u6ee1\u6743\u8861\u7684\u8fc7\u7a0b\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u83b7\u5f97\u67d0\u65b9\u9762\u7684\u4f18\u52bf\uff0c\u5219\u5f80\u5f80\u9700\u8981\u5728\u53e6\u4e00\u65b9\u9762\u505a\u51fa\u59a5\u534f\u3002\u4f8b\u5982\uff0c\u94fe\u8868\u76f8\u5bf9\u4e8e\u6570\u7ec4\uff0c\u6570\u636e\u6dfb\u52a0\u5220\u9664\u64cd\u4f5c\u66f4\u52a0\u65b9\u4fbf\uff0c\u4f46\u727a\u7272\u4e86\u6570\u636e\u7684\u8bbf\u95ee\u901f\u5ea6\uff1b\u56fe\u76f8\u5bf9\u4e8e\u94fe\u8868\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u903b\u8f91\u4fe1\u606f\uff0c\u4f46\u9700\u8981\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u7a7a\u95f4\u3002</p>"},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3. \u00a0 \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb","text":"<p>\u300c\u6570\u636e\u7ed3\u6784\u300d\u4e0e\u300c\u7b97\u6cd5\u300d\u662f\u9ad8\u5ea6\u76f8\u5173\u3001\u7d27\u5bc6\u5d4c\u5408\u7684\uff0c\u4f53\u73b0\u5728\uff1a</p> <ul> <li>\u6570\u636e\u7ed3\u6784\u662f\u7b97\u6cd5\u7684\u5e95\u5ea7\u3002\u6570\u636e\u7ed3\u6784\u4e3a\u7b97\u6cd5\u63d0\u4f9b\u7ed3\u6784\u5316\u5b58\u50a8\u7684\u6570\u636e\uff0c\u4ee5\u53ca\u64cd\u4f5c\u6570\u636e\u7684\u5bf9\u5e94\u65b9\u6cd5\u3002</li> <li>\u7b97\u6cd5\u662f\u6570\u636e\u7ed3\u6784\u53d1\u6325\u7684\u821e\u53f0\u3002\u6570\u636e\u7ed3\u6784\u4ec5\u5b58\u50a8\u6570\u636e\u4fe1\u606f\uff0c\u7ed3\u5408\u7b97\u6cd5\u624d\u53ef\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u3002</li> <li>\u7b97\u6cd5\u6709\u5bf9\u5e94\u6700\u4f18\u7684\u6570\u636e\u7ed3\u6784\u3002\u7ed9\u5b9a\u7b97\u6cd5\uff0c\u4e00\u822c\u53ef\u57fa\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u73b0\uff0c\u800c\u6700\u7ec8\u6267\u884c\u6548\u7387\u5f80\u5f80\u76f8\u5dee\u5f88\u5927\u3002</li> </ul> <p></p> <p> Fig. \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u5173\u7cfb </p> <p>\u5982\u679c\u5c06\u300cLEGO \u4e50\u9ad8\u300d\u7c7b\u6bd4\u5230\u300c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300d\uff0c\u90a3\u4e48\u53ef\u4ee5\u5f97\u5230\u4e0b\u8868\u6240\u793a\u7684\u5bf9\u5e94\u5173\u7cfb\u3002</p> \u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5 LEGO \u4e50\u9ad8 \u8f93\u5165\u6570\u636e \u672a\u62fc\u88c5\u7684\u79ef\u6728 \u6570\u636e\u7ed3\u6784 \u79ef\u6728\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u5305\u62ec\u5f62\u72b6\u3001\u5927\u5c0f\u3001\u8fde\u63a5\u65b9\u5f0f\u7b49 \u7b97\u6cd5 \u628a\u79ef\u6728\u62fc\u6210\u76ee\u6807\u5f62\u6001\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u6b65\u9aa4 \u8f93\u51fa\u6570\u636e \u79ef\u6728\u6a21\u578b <p>\u7ea6\u5b9a\u4fd7\u6210\u7684\u7b80\u79f0</p> <p>\u5728\u5b9e\u9645\u8ba8\u8bba\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u300c\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u300d\u76f4\u63a5\u7b80\u79f0\u4e3a\u300c\u7b97\u6cd5\u300d\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u719f\u79f0\u7684 LeetCode \u7b97\u6cd5\u9898\u76ee\uff0c\u5b9e\u9645\u4e0a\u540c\u65f6\u8003\u5bdf\u4e86\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e24\u90e8\u5206\u77e5\u8bc6\u3002</p>"},{"location":"chapter_preface/about_the_book/","title":"0.1. \u00a0 \u5173\u4e8e\u672c\u4e66","text":"<p>Tip</p> <p>\u5bf9\u4e8e\u672c\u7ae0\u5185\u5bb9\uff0c\u5efa\u8bae\u901a\u8bfb\u5982\u4f55\u4f7f\u7528\u672c\u4e66\uff0c\u5176\u4f59\u7ae0\u8282\u53ef\u6839\u636e\u9700\u6c42\u9009\u8bfb\u3002</p> <p>\u4e94\u5e74\u524d\u53d1\u751f\u7684\u4e00\u4ef6\u4e8b\uff0c\u6210\u4e3a\u4e86\u6211\u804c\u4e1a\u751f\u6daf\u7684\u91cd\u8981\u8f6c\u6298\u70b9\u3002\u5f53\u65f6\u7684\u6211\u5728\u4ea4\u5927\u8bfb\u7814\uff0c\u5bf9\u4e92\u8054\u7f51\u6c42\u804c\u4e00\u65e0\u6240\u77e5\uff0c\u4f46\u4ecd\u7136\u786c\u7740\u5934\u76ae\u7533\u8bf7\u4e86 Microsoft \u8f6f\u4ef6\u5de5\u7a0b\u5e08\u5b9e\u4e60\u3002\u9762\u8bd5\u5b98\u8ba9\u6211\u5728\u767d\u677f\u4e0a\u5199\u51fa\u201c\u5feb\u901f\u6392\u5e8f\u201d\u4ee3\u7801\uff0c\u6211\u754f\u754f\u7f29\u7f29\u5730\u5199\u4e86\u4e00\u4e2a\u201c\u5192\u6ce1\u6392\u5e8f\u201d\uff0c\u5e76\u4e14\u8fd8\u5199\u9519\u4e86<code>(ToT)</code> \u3002\u4ece\u9762\u8bd5\u5b98\u7684\u8868\u60c5\u4e0a\uff0c\u6211\u770b\u5230\u4e86\u4e00\u4e2a\u5927\u5927\u7684 \"GG\" \u3002</p> <p>\u6b64\u6b21\u5931\u5229\u5012\u903c\u6211\u5f00\u59cb\u5237\u7b97\u6cd5\u9898\u3002\u6211\u91c7\u7528\u201c\u626b\u96f7\u6e38\u620f\u201d\u5f0f\u7684\u5b66\u4e60\u65b9\u6cd5\uff0c\u4e24\u773c\u4e00\u62b9\u9ed1\u5237\u9898\uff0c\u626b\u5230\u4e0d\u4f1a\u7684\u201c\u96f7\u201d\u5c31\u901a\u8fc7\u67e5\u8d44\u6599\u628a\u5b83\u201c\u6392\u6389\u201d\uff0c\u914d\u5408\u5468\u671f\u6027\u603b\u7ed3\uff0c\u9010\u6e10\u5f62\u6210\u4e86\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u77e5\u8bc6\u56fe\u666f\u3002\u5e78\u8fd0\u5730\uff0c\u6211\u5728\u79cb\u62db\u65a9\u83b7\u4e86\u591a\u5bb6\u5927\u5382\u7684 Offer \u3002</p> <p>\u56de\u60f3\u81ea\u5df1\u5f53\u521d\u5728\u201c\u626b\u96f7\u5f0f\u201d\u5237\u9898\u4e2d\u88ab\u70b8\u7684\u6ee1\u5934\u5305\u7684\u75db\u82e6\uff0c\u601d\u8003\u826f\u4e45\uff0c\u6211\u610f\u8bc6\u5230\u4e00\u672c\u201c\u524d\u671f\u5237\u9898\u5fc5\u770b\u201d\u7684\u8bfb\u7269\u53ef\u4ee5\u4f7f\u7b97\u6cd5\u5c0f\u767d\u5c11\u8d70\u8bb8\u591a\u5f2f\u8def\u3002\u5199\u4f5c\u610f\u613f\u6eda\u6eda\u88ad\u6765\uff0c\u90a3\u5c31\u52a8\u7b14\u5427\uff1a</p> Hello\uff0c\u7b97\u6cd5\uff01"},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1. \u00a0 \u8bfb\u8005\u5bf9\u8c61","text":"<p>\u5982\u679c\u60a8\u662f\u300c\u7b97\u6cd5\u521d\u5b66\u8005\u300d\uff0c\u5b8c\u5168\u6ca1\u6709\u63a5\u89e6\u8fc7\u7b97\u6cd5\uff0c\u6216\u8005\u5df2\u7ecf\u6709\u5c11\u91cf\u5237\u9898\uff0c\u5bf9\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u6709\u6726\u80e7\u7684\u7406\u89e3\uff0c\u5728\u4f1a\u4e0e\u4e0d\u4f1a\u4e4b\u95f4\u53cd\u590d\u6a2a\u8df3\uff0c\u90a3\u4e48\u8fd9\u672c\u4e66\u5c31\u662f\u4e3a\u60a8\u800c\u5199\uff01\u672c\u4e66\u80fd\u591f\u5e26\u6765\uff1a</p> <ul> <li>\u5e26\u9886\u4f60\u5b8c\u6574\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\uff0c\u521d\u6b65\u642d\u5efa\u77e5\u8bc6\u6846\u67b6\uff0c\u4f7f\u5237\u9898\u4e8b\u534a\u529f\u500d\uff1b</li> <li>\u63d0\u4f9b\u5199\u6cd5\u6807\u51c6\u6574\u6d01\u3001\u53ef\u4e00\u952e\u8fd0\u884c\u7684\u914d\u5957\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u517b\u6210\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\uff1b</li> </ul> <p>\u5982\u679c\u60a8\u662f\u300c\u7b97\u6cd5\u8001\u624b\u300d\uff0c\u5df2\u7ecf\u79ef\u7d2f\u4e00\u5b9a\u5237\u9898\u91cf\uff0c\u63a5\u89e6\u8fc7\u5927\u591a\u6570\u9898\u578b\uff0c\u90a3\u4e48\u672c\u4e66\u5185\u5bb9\u5bf9\u4f60\u6765\u8bf4\u53ef\u80fd\u7a0d\u663e\u57fa\u7840\uff0c\u4f46\u4ecd\u80fd\u591f\u5e26\u6765\u4ee5\u4e0b\u4ef7\u503c\uff1a</p> <ul> <li>\u672c\u4e66\u7bc7\u5e45\u4e0d\u957f\uff0c\u5e76\u4e14\u5305\u542b\u8f83\u591a\u5bf9\u6bd4\u6027\u548c\u603b\u7ed3\u6027\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u56de\u987e\u4e0e\u68b3\u7406\u7b97\u6cd5\u77e5\u8bc6\u4f53\u7cfb\uff1b</li> <li>\u6e90\u4ee3\u7801\u5b9e\u73b0\u4e86\u5404\u79cd\u7ecf\u5178\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u201c\u5237\u9898\u5de5\u5177\u5e93\u201d\u6216\u201c\u7b97\u6cd5\u5b9e\u73b0\u5b57\u5178\u201d\u6765\u4f7f\u7528\uff1b</li> </ul> <p>\u5982\u679c\u60a8\u662f\u300c\u7b97\u6cd5\u5927\u4f6c\u300d\uff0c\u8bf7\u53d7\u6211\u819c\u62dc\uff01\u5e0c\u671b\u60a8\u53ef\u4ee5\u62bd\u65f6\u95f4\u63d0\u51fa\u610f\u89c1\u5efa\u8bae\uff0c\u6216\u8005\u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c\uff0c\u5e2e\u52a9\u5404\u4f4d\u540c\u5b66\u83b7\u53d6\u66f4\u597d\u7684\u5b66\u4e60\u5185\u5bb9\uff0c\u611f\u8c22\uff01</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\u5206\u4e3a\u590d\u6742\u5ea6\u5206\u6790\u3001\u6570\u636e\u7ed3\u6784\u3001\u7b97\u6cd5\u4e09\u4e2a\u90e8\u5206\u3002</p> <p></p> <p> Fig. Hello \u7b97\u6cd5\u5185\u5bb9\u7ed3\u6784 </p>"},{"location":"chapter_preface/about_the_book/#_1","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u9996\u5148\u4ecb\u7ecd\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8bc4\u4ef7\u7ef4\u5ea6\u3001\u7b97\u6cd5\u6548\u7387\u7684\u8bc4\u4f30\u65b9\u6cd5\uff0c\u5f15\u51fa\u4e86\u8ba1\u7b97\u590d\u6742\u5ea6\u6982\u5ff5\u3002</p> <p>\u63a5\u4e0b\u6765\uff0c\u4ece \u51fd\u6570\u6e10\u8fd1\u4e0a\u754c \u5165\u624b\uff0c\u5206\u522b\u4ecb\u7ecd\u4e86 \u65f6\u95f4\u590d\u6742\u5ea6 \u548c \u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u5305\u62ec\u63a8\u7b97\u65b9\u6cd5\u3001\u5e38\u89c1\u7c7b\u578b\u3001\u793a\u4f8b\u7b49\u3002\u540c\u65f6\uff0c\u5256\u6790\u4e86 \u6700\u5dee\u3001\u6700\u4f73\u3001\u5e73\u5747 \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u8054\u7cfb\u4e0e\u533a\u522b\u3002</p>"},{"location":"chapter_preface/about_the_book/#_2","title":"\u6570\u636e\u7ed3\u6784","text":"<p>\u9996\u5148\u4ecb\u7ecd\u4e86\u5e38\u7528\u7684 \u57fa\u672c\u6570\u636e\u7c7b\u578b \u3001\u4ee5\u53ca\u5b83\u4eec\u662f\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u3002</p> <p>\u63a5\u4e0b\u6765\uff0c\u4ecb\u7ecd\u4e86\u4e24\u79cd \u6570\u636e\u7ed3\u6784\u5206\u7c7b\u65b9\u6cd5\uff0c\u5305\u62ec\u903b\u8f91\u7ed3\u6784\u4e0e\u7269\u7406\u7ed3\u6784\u3002</p> <p>\u540e\u7eed\u5c55\u5f00\u4ecb\u7ecd\u4e86 \u6570\u7ec4\u3001\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u6563\u5217\u8868\u3001\u6811\u3001\u5806\u3001\u56fe \u7b49\u6570\u636e\u7ed3\u6784\uff0c\u5173\u5fc3\u4ee5\u4e0b\u5185\u5bb9\uff1a</p> <ul> <li>\u57fa\u672c\u5b9a\u4e49\uff1a\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u6765\u6e90\u3001\u5b58\u5728\u610f\u4e49\uff1b</li> <li>\u4e3b\u8981\u7279\u70b9\uff1a\u5728\u5404\u9879\u6570\u636e\u64cd\u4f5c\u4e2d\u7684\u4f18\u52bf\u3001\u52a3\u52bf\uff1b</li> <li>\u5e38\u7528\u64cd\u4f5c\uff1a\u4f8b\u5982\u8bbf\u95ee\u3001\u66f4\u65b0\u3001\u63d2\u5165\u3001\u5220\u9664\u3001\u904d\u5386\u3001\u641c\u7d22\u7b49\uff1b</li> <li>\u5e38\u89c1\u7c7b\u578b\uff1a\u5728\u7b97\u6cd5\u9898\u6216\u5de5\u7a0b\u5b9e\u9645\u4e2d\uff0c\u7ecf\u5e38\u78b0\u5230\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u578b\uff1b</li> <li>\u5178\u578b\u5e94\u7528\uff1a\u6b64\u6570\u636e\u7ed3\u6784\u7ecf\u5e38\u642d\u914d\u54ea\u4e9b\u7b97\u6cd5\u4f7f\u7528\uff1b</li> <li>\u5b9e\u73b0\u65b9\u6cd5\uff1a\u5bf9\u4e8e\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5c06\u7ed9\u51fa\u5b8c\u6574\u7684\u5b9e\u73b0\u793a\u4f8b\uff1b</li> </ul>"},{"location":"chapter_preface/about_the_book/#_3","title":"\u7b97\u6cd5","text":"<p>\u5305\u62ec \u67e5\u627e\u7b97\u6cd5\u3001\u6392\u5e8f\u7b97\u6cd5\u3001\u641c\u7d22\u4e0e\u56de\u6eaf\u3001\u52a8\u6001\u89c4\u5212\u3001\u5206\u6cbb\u7b97\u6cd5\uff0c\u5185\u5bb9\u5305\u62ec\uff1a</p> <ul> <li>\u57fa\u672c\u5b9a\u4e49\uff1a\u7b97\u6cd5\u7684\u8bbe\u8ba1\u601d\u60f3\uff1b</li> <li>\u4e3b\u8981\u7279\u70b9\uff1a\u4f7f\u7528\u524d\u7f6e\u6761\u4ef6\u3001\u4f18\u52bf\u548c\u52a3\u52bf\uff1b</li> <li>\u7b97\u6cd5\u6548\u7387\uff1a\u6700\u5dee\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u3001\u7a7a\u95f4\u590d\u6742\u5ea6\uff1b</li> <li>\u5b9e\u73b0\u65b9\u6cd5\uff1a\u5b8c\u6574\u7684\u7b97\u6cd5\u5b9e\u73b0\uff0c\u4ee5\u53ca\u4f18\u5316\u63aa\u65bd\uff1b</li> <li>\u793a\u4f8b\u9898\u76ee\uff1a\u7ed3\u5408\u4f8b\u9898\u52a0\u6df1\u7406\u89e3\uff1b</li> </ul>"},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3. \u00a0 \u672c\u4e66\u5b9a\u4f4d","text":"<p>\u603b\u4f53\u4e0a\u770b\uff0c\u6211\u8ba4\u4e3a\u53ef\u5c06\u5b66\u4e60\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\u3002</p> <ul> <li>\u7b2c\u4e00\u9636\u6bb5\uff0c\u7b97\u6cd5\u5165\u95e8\u3002\u719f\u6089\u5404\u79cd\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\u3001\u7528\u6cd5\uff0c\u5b66\u4e60\u5404\u79cd\u7b97\u6cd5\u7684\u539f\u7406\u3001\u6d41\u7a0b\u3001\u7528\u9014\u3001\u6548\u7387\u7b49\u3002</li> <li>\u7b2c\u4e8c\u9636\u6bb5\uff0c\u5237\u7b97\u6cd5\u9898\u3002\u53ef\u4ee5\u5148\u4ece\u70ed\u95e8\u9898\u5355\u5f00\u5237\uff0c\u63a8\u8350\u5251\u6307 Offer\u3001LeetCode Hot 100\uff0c\u5148\u79ef\u7d2f\u81f3\u5c11 100 \u9053\u9898\u91cf\uff0c\u719f\u6089\u5927\u591a\u6570\u7684\u7b97\u6cd5\u95ee\u9898\u3002\u521a\u5f00\u59cb\u5237\u9898\u65f6\uff0c\u201c\u9057\u5fd8\u201d\u662f\u6700\u5927\u7684\u56f0\u6270\u70b9\uff0c\u4f46\u8fd9\u662f\u5f88\u6b63\u5e38\u7684\uff0c\u8bf7\u4e0d\u8981\u62c5\u5fc3\u3002\u5b66\u4e60\u4e2d\u6709\u4e00\u79cd\u6982\u5ff5\u53eb\u201c\u5468\u671f\u6027\u56de\u987e\u201d\uff0c\u540c\u4e00\u9053\u9898\u9694\u6bb5\u65f6\u95f4\u505a\u4e00\u6b21\uff0c\u5728\u91cd\u590d 3 \u8f6e\u4ee5\u4e0a\u540e\uff0c\u5f80\u5f80\u5c31\u80fd\u7262\u8bb0\u4e8e\u5fc3\u4e86\u3002</li> <li>\u7b2c\u4e09\u9636\u6bb5\uff0c\u642d\u5efa\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5b66\u4e60\u65b9\u9762\uff0c\u53ef\u4ee5\u9605\u8bfb\u7b97\u6cd5\u4e13\u680f\u6587\u7ae0\u3001\u89e3\u9898\u6846\u67b6\u3001\u7b97\u6cd5\u6559\u6750\uff0c\u4e0d\u65ad\u5730\u4e30\u5bcc\u77e5\u8bc6\u4f53\u7cfb\u3002\u5728\u5237\u9898\u65b9\u9762\uff0c\u53ef\u4ee5\u5f00\u59cb\u91c7\u7528\u8fdb\u9636\u5237\u9898\u65b9\u6848\uff0c\u4f8b\u5982\u6309\u4e13\u9898\u5206\u7c7b\u3001\u4e00\u9898\u591a\u89e3\u3001\u4e00\u89e3\u591a\u9898\u7b49\uff0c\u76f8\u5e94\u5237\u9898\u8ba1\u5212\u4e0e\u5fc3\u5f97\u53ef\u4ee5\u5728\u793e\u533a\u4e2d\u627e\u5230\uff0c\u5728\u6b64\u4e0d\u505a\u8d58\u8ff0\u3002</li> </ul> <p>\u6839\u636e\u89c2\u5bdf\uff0c\u5f88\u591a\u540c\u5b66\u90fd\u662f\u4ece\u201c\u7b2c\u4e8c\u9636\u6bb5\u201d\u5f00\u59cb\u5b66\u4e60\u7b97\u6cd5\u7684\u3002\u800c\u4f5c\u4e3a\u5165\u95e8\u6559\u7a0b\uff0c\u672c\u4e66\u5185\u5bb9\u4e3b\u8981\u5bf9\u5e94\u201c\u7b2c\u4e00\u9636\u6bb5\u201d\uff0c\u81f4\u529b\u4e8e\u5e2e\u52a9\u8bfb\u8005\u66f4\u9ad8\u6548\u5730\u5f00\u5c55\u7b2c\u4e8c\u3001\u4e09\u9636\u6bb5\u7684\u5b66\u4e60\u3002</p> <p></p> <p> Fig. \u7b97\u6cd5\u5b66\u4e60\u8def\u7ebf </p>"},{"location":"chapter_preface/about_the_book/#014","title":"0.1.4. \u00a0 \u672c\u4e66\u7279\u70b9","text":"<p>\u4ee5\u5b9e\u8df5\u4e3a\u4e3b\u3002\u6211\u4eec\u77e5\u9053\uff0c\u5b66\u4e60\u82f1\u8bed\u671f\u95f4\u5149\u5543\u4e66\u672c\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684\uff0c\u9700\u8981\u591a\u542c\u3001\u591a\u8bf4\u3001\u591a\u5199\uff0c\u5728\u5b9e\u8df5\u4e2d\u57f9\u517b\u8bed\u611f\u3001\u79ef\u7d2f\u7ecf\u9a8c\u3002\u7f16\u7a0b\u8bed\u8a00\u4e5f\u662f\u4e00\u95e8\u8bed\u8a00\uff0c\u56e0\u6b64\u5b66\u4e60\u65b9\u6cd5\u4e5f\u5e94\u662f\u7c7b\u4f3c\u7684\uff0c\u9700\u8981\u591a\u770b\u4f18\u79c0\u4ee3\u7801\u3001\u591a\u6572\u952e\u76d8\u3001\u591a\u601d\u8003\u4ee3\u7801\u903b\u8f91\u3002</p> <p>\u672c\u4e66\u7684\u7406\u8bba\u90e8\u5206\u5360\u5c11\u91cf\u7bc7\u5e45\uff0c\u4e3b\u8981\u5206\u4e3a\u4e24\u7c7b\uff1a\u4e00\u662f\u57fa\u7840\u4e14\u5fc5\u8981\u7684\u6982\u5ff5\u77e5\u8bc6\uff0c\u4ee5\u57f9\u517b\u8bfb\u8005\u5bf9\u4e8e\u7b97\u6cd5\u7684\u611f\u6027\u8ba4\u8bc6\uff1b\u4e8c\u662f\u91cd\u8981\u7684\u5206\u7c7b\u3001\u5bf9\u6bd4\u6216\u603b\u7ed3\uff0c\u8fd9\u662f\u4e3a\u4e86\u5e2e\u52a9\u4f60\u7ad9\u5728\u66f4\u9ad8\u89c6\u89d2\u4fef\u77b0\u5404\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5f62\u6210\u8fde\u70b9\u6210\u9762\u7684\u6548\u679c\u3002</p> <p>\u5b9e\u8df5\u90e8\u5206\u4e3b\u8981\u7531\u793a\u4f8b\u548c\u4ee3\u7801\u7ec4\u6210\u3002\u4ee3\u7801\u914d\u6709\u7b80\u8981\u6ce8\u91ca\uff0c\u590d\u6742\u793a\u4f8b\u4f1a\u5c3d\u53ef\u80fd\u5730\u4f7f\u7528\u89c6\u89c9\u5316\u7684\u5f62\u5f0f\u5448\u73b0\u3002\u6211\u5f3a\u70c8\u5efa\u8bae\u8bfb\u8005\u5bf9\u7167\u7740\u4ee3\u7801\u81ea\u5df1\u6572\u4e00\u904d\uff0c\u5982\u679c\u65f6\u95f4\u6709\u9650\uff0c\u4e5f\u81f3\u5c11\u9010\u884c\u8bfb\u3001\u590d\u5236\u5e76\u8fd0\u884c\u4e00\u904d\uff0c\u914d\u5408\u7740\u8bb2\u89e3\u5c06\u4ee3\u7801\u5403\u900f\u3002</p> <p>\u89c6\u89c9\u5316\u5b66\u4e60\u3002\u4fe1\u606f\u65f6\u4ee3\u4ee5\u6765\uff0c\u89c6\u89c9\u5316\u7684\u811a\u6b65\u4ece\u672a\u505c\u6b62\u3002\u5a92\u4f53\u5f62\u5f0f\u7ecf\u5386\u4e86\u6587\u5b57\u77ed\u4fe1\u3001\u56fe\u6587 Email \u3001\u52a8\u56fe\u3001\u77ed\uff08\u957f\uff09\u89c6\u9891\u3001\u4ea4\u4e92\u5f0f Web \u30013D \u6e38\u620f\u7b49\u6f14\u53d8\u8fc7\u7a0b\uff0c\u4fe1\u606f\u7684\u89c6\u89c9\u5316\u7a0b\u5ea6\u8d8a\u6765\u8d8a\u9ad8\u3001\u6108\u52a0\u7b26\u5408\u4eba\u7c7b\u611f\u5b98\u3001\u4fe1\u606f\u4f20\u64ad\u6548\u7387\u5927\u5927\u63d0\u5347\u3002\u79d1\u6280\u754c\u4e5f\u5728\u5411\u89c6\u89c9\u5316\u8fc8\u8fdb\uff0ciPhone \u5c31\u662f\u4e00\u4e2a\u5178\u578b\u4f8b\u5b50\uff0c\u5176\u76f8\u5bf9\u4e8e\u4f20\u7edf\u624b\u673a\u662f\u9ad8\u5ea6\u89c6\u89c9\u5316\u7684\uff0c\u5305\u542b\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u5b57\u4f53\u3001\u4e3b\u9898\u914d\u8272\u3001\u4ea4\u4e92\u52a8\u753b\u7b49\u3002</p> <p>\u8fd1\u4e24\u5e74\uff0c\u77ed\u89c6\u9891\u6210\u4e3a\u6700\u53d7\u6b22\u8fce\u7684\u4fe1\u606f\u5a92\u4ecb\uff0c\u53ef\u4ee5\u5728\u77ed\u65f6\u95f4\u5185\u5c06\u9ad8\u5bc6\u5ea6\u7684\u4fe1\u606f\u201c\u704c\u201d\u7ed9\u6211\u4eec\uff0c\u6709\u7740\u6781\u5176\u8212\u9002\u7684\u89c2\u770b\u4f53\u9a8c\u3002\u9605\u8bfb\u5219\u4e0d\u7136\uff0c\u8bfb\u8005\u4e0e\u4e66\u672c\u4e4b\u95f4\u5929\u7136\u5b58\u5728\u4e00\u79cd\u201c\u758f\u79bb\u611f\u201d\uff0c\u6211\u4eec\u770b\u4e66\u4f1a\u7d2f\u3001\u4f1a\u8d70\u795e\u3001\u4f1a\u505c\u4e0b\u6765\u60f3\u5176\u4ed6\u4e8b\u3001\u4f1a\u5212\u4e0b\u559c\u6b22\u7684\u53e5\u5b50\u3001\u4f1a\u601d\u8003\u67d0\u4e00\u7247\u6bb5\u7684\u542b\u4e49\uff0c\u8fd9\u79cd\u758f\u79bb\u611f\u7ed9\u4e86\u8bfb\u8005\u4e0e\u4e66\u672c\u4e4b\u95f4\u5bf9\u8bdd\u7684\u53ef\u80fd\uff0c\u62d3\u5bbd\u4e86\u60f3\u8c61\u7a7a\u95f4\u3002</p> <p>\u672c\u4e66\u4f5c\u4e3a\u4e00\u672c\u5165\u95e8\u6559\u6750\uff0c\u5e0c\u671b\u53ef\u4ee5\u4fdd\u6709\u4e66\u672c\u7684\u201c\u6162\u8282\u594f\u201d\uff0c\u4f46\u4e5f\u4f1a\u907f\u514d\u4e0e\u8bfb\u8005\u4ea7\u751f\u8fc7\u591a\u201c\u758f\u79bb\u611f\u201d\uff0c\u800c\u662f\u52aa\u529b\u5c06\u77e5\u8bc6\u5b8c\u6574\u6e05\u6670\u5730\u63a8\u9001\u5230\u4f60\u806a\u660e\u7684\u5c0f\u8111\u888b\u74dc\u4e2d\u3002\u6211\u5c06\u91c7\u7528\u89c6\u89c9\u5316\u7684\u65b9\u5f0f\uff08\u4f8b\u5982\u914d\u56fe\u3001\u52a8\u753b\uff09\uff0c\u5c3d\u6211\u53ef\u80fd\u6e05\u6670\u6613\u61c2\u5730\u8bb2\u89e3\u590d\u6742\u6982\u5ff5\u548c\u62bd\u8c61\u793a\u4f8b\u3002</p> <p>\u5185\u5bb9\u7cbe\u7b80\u5316\u3002\u5927\u591a\u6570\u7684\u7ecf\u5178\u6559\u79d1\u4e66\uff0c\u4f1a\u628a\u6bcf\u4e2a\u4e3b\u9898\u90fd\u8bb2\u7684\u5f88\u900f\u5f7b\u3002\u867d\u7136\u900f\u5f7b\u6027\u6b63\u662f\u5176\u83b7\u5f97\u8bfb\u8005\u9752\u7750\u7684\u539f\u56e0\uff0c\u4f46\u5bf9\u4e8e\u60f3\u8981\u5feb\u901f\u5165\u95e8\u7684\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u8fd9\u4e9b\u6559\u6750\u7684\u5b9e\u7528\u6027\u4e0d\u8db3\u3002\u672c\u4e66\u4f1a\u907f\u514d\u5f15\u5165\u975e\u5fc5\u8981\u7684\u6982\u5ff5\u3001\u540d\u8bcd\u3001\u5b9a\u4e49\u7b49\uff0c\u4e5f\u907f\u514d\u5c55\u5f00\u4e0d\u5fc5\u8981\u7684\u7406\u8bba\u5206\u6790\uff0c\u6bd5\u7adf\u8fd9\u4e0d\u662f\u4e00\u672c\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u6559\u6750\uff0c\u4e3b\u8981\u4efb\u52a1\u662f\u5c3d\u5feb\u5730\u5e26\u9886\u8bfb\u8005\u5165\u95e8\u3002</p> <p>\u5f15\u5165\u4e00\u4e9b\u751f\u6d3b\u6848\u4f8b\u6216\u8da3\u5473\u5185\u5bb9\uff0c\u975e\u5e38\u9002\u5408\u4f5c\u4e3a\u77e5\u8bc6\u70b9\u7684\u5f15\u5b50\u6216\u8005\u89e3\u91ca\u7684\u8865\u5145\uff0c\u4f46\u5f53\u878d\u5165\u8fc7\u591a\u989d\u5916\u5143\u7d20\u65f6\uff0c\u5185\u5bb9\u4f1a\u7a0d\u663e\u5197\u957f\uff0c\u4e5f\u8bb8\u53cd\u800c\u4f7f\u8bfb\u8005\u5bb9\u6613\u8ff7\u5931\u3001\u6293\u4e0d\u4f4f\u91cd\u70b9\uff0c\u8fd9\u4e5f\u662f\u672c\u4e66\u9700\u8981\u907f\u514d\u7684\u3002</p> <p>\u6572\u4ee3\u7801\u5982\u540c\u5199\u5b57\uff0c\u201c\u7f8e\u201d\u662f\u7edf\u4e00\u7684\u8ffd\u6c42\u3002\u672c\u4e66\u529b\u6c42\u7f8e\u89c2\u7684\u4ee3\u7801\uff0c\u4fdd\u8bc1\u89c4\u8303\u7684\u53d8\u91cf\u547d\u540d\u3001\u7edf\u4e00\u7684\u7a7a\u683c\u4e0e\u6362\u884c\u3001\u5bf9\u9f50\u7684\u7f29\u8fdb\u3001\u6574\u9f50\u7684\u6ce8\u91ca\u7b49\u3002</p>"},{"location":"chapter_preface/about_the_book/#015","title":"0.1.5. \u00a0 \u81f4\u8c22","text":"<p>\u672c\u4e66\u7684\u6210\u4e66\u8fc7\u7a0b\u4e2d\uff0c\u6211\u83b7\u5f97\u4e86\u8bb8\u591a\u4eba\u7684\u5e2e\u52a9\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a</p> <ul> <li>\u611f\u8c22\u6211\u7684\u5973\u670b\u53cb\u6ce1\u6ce1\u62c5\u4efb\u672c\u4e66\u7684\u9996\u4f4d\u8bfb\u8005\uff0c\u4ece\u7b97\u6cd5\u5c0f\u767d\u7684\u89c6\u89d2\u4e3a\u672c\u4e66\u7684\u5199\u4f5c\u63d0\u51fa\u4e86\u8bb8\u591a\u5efa\u8bae\uff0c\u4f7f\u8fd9\u672c\u4e66\u66f4\u52a0\u9002\u5408\u7b97\u6cd5\u521d\u5b66\u8005\u6765\u9605\u8bfb\u3002</li> <li>\u611f\u8c22\u817e\u5b9d\u3001\u7426\u5b9d\u3001\u98de\u5b9d\u4e3a\u672c\u4e66\u8d77\u4e86\u4e2a\u54cd\u5f53\u5f53\u7684\u540d\u5b57\uff0c\u597d\u542c\u53c8\u6709\u6897\uff0c\u76f4\u63a5\u5524\u8d77\u6211\u6700\u521d\u6572\u4e0b\u7b2c\u4e00\u884c\u4ee3\u7801 \"Hello, World!\" \u7684\u56de\u5fc6\u3002</li> <li>\u611f\u8c22\u6211\u7684\u5bfc\u5e08\u674e\u535a\uff0c\u5728\u5c0f\u914c\u7545\u8c08\u65f6\u60a8\u544a\u8bc9\u6211\u201c\u89c9\u5f97\u5e94\u8be5\u505a\u5c31\u53bb\u505a\u201d\uff0c\u575a\u5b9a\u4e86\u6211\u5199\u8fd9\u672c\u4e66\u7684\u51b3\u5fc3\u3002</li> <li>\u611f\u8c22\u82cf\u6f7c\u4e3a\u672c\u4e66\u8bbe\u8ba1\u4e86\u5c01\u9762\u548c LOGO \uff0c\u6211\u6709\u4e9b\u5f3a\u8feb\u75c7\uff0c\u524d\u540e\u591a\u6b21\u4fee\u6539\uff0c\u8c22\u8c22\u4f60\u7684\u8010\u5fc3\u3002</li> <li>\u611f\u8c22 @squidfunk \u7ed9\u51fa\u7684\u5199\u4f5c\u6392\u7248\u5efa\u8bae\uff0c\u4ee5\u53ca\u4f18\u79c0\u5f00\u6e90\u9879\u76ee Material-for-MkDocs \u3002</li> </ul> <p>\u672c\u4e66\u9f13\u52b1\u201c\u624b\u8111\u5e76\u7528\u201d\u7684\u5b66\u4e60\u65b9\u5f0f\uff0c\u5728\u8fd9\u70b9\u4e0a\u53d7\u5230\u4e86\u300a\u52a8\u624b\u5b66\u6df1\u5ea6\u5b66\u4e60\u300b\u5f88\u5927\u5f71\u54cd\uff0c\u4e5f\u5728\u6b64\u5411\u5404\u4f4d\u540c\u5b66\u5f3a\u70c8\u63a8\u8350\u8fd9\u672c\u8457\u4f5c\uff0c\u5305\u62ec\u4e2d\u6587\u7248\u3001\u82f1\u6587\u7248\u3001\u674e\u6c90\u8001\u5e08 bilibili \u4e3b\u9875\u3002</p> <p>\u5728\u5199\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u6211\u9605\u8bfb\u4e86\u8bb8\u591a\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u7684\u6559\u6750\u4e0e\u6587\u7ae0\uff0c\u8fd9\u4e9b\u8457\u4f5c\u4e3a\u672c\u4e66\u4f5c\u51fa\u4e86\u5f88\u597d\u7684\u699c\u6837\uff0c\u4fdd\u8bc1\u4e86\u672c\u4e66\u5185\u5bb9\u7684\u6b63\u786e\u6027\u4e0e\u8d28\u91cf\uff0c\u611f\u8c22\u524d\u8f88\u4eec\u7684\u7cbe\u5f69\u521b\u4f5c\uff01</p> <p>\u611f\u8c22\u7236\u6bcd\uff0c\u4f60\u4eec\u4e00\u8d2f\u7684\u652f\u6301\u4e0e\u9f13\u52b1\u7ed9\u4e86\u6211\u81ea\u7531\u5ea6\u6765\u505a\u8fd9\u4e9b\u6709\u8da3\u7684\u4e8b\u3002</p>"},{"location":"chapter_preface/contribution/","title":"0.4. \u00a0 \u4e00\u8d77\u53c2\u4e0e\u521b\u4f5c","text":"<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\u4f46\u5728\u672c\u5f00\u6e90 HTML \u4e66\u4e2d\uff0c\u5185\u5bb9\u66f4\u8fed\u7684\u65f6\u95f4\u88ab\u7f29\u77ed\u81f3\u6570\u65e5\u751a\u81f3\u51e0\u4e2a\u5c0f\u65f6\u3002</p> <p>\u7531\u4e8e\u4f5c\u8005\u6c34\u5e73\u6709\u9650\uff0c\u4e66\u4e2d\u5185\u5bb9\u96be\u514d\u758f\u6f0f\u8c2c\u8bef\uff0c\u8bf7\u60a8\u8c05\u89e3\u3002\u5982\u679c\u53d1\u73b0\u7b14\u8bef\u3001\u65e0\u6548\u94fe\u63a5\u3001\u5185\u5bb9\u7f3a\u5931\u3001\u6587\u5b57\u6b67\u4e49\u3001\u89e3\u91ca\u4e0d\u6e05\u6670\u3001\u884c\u6587\u7ed3\u6784\u4e0d\u5408\u7406\u7b49\u95ee\u9898\uff0c\u8bf7\u60a8\u5e2e\u5fd9\u4fee\u6b63\uff0c\u4ee5\u5e2e\u52a9\u5176\u4ed6\u8bfb\u8005\u83b7\u53d6\u66f4\u4f18\u8d28\u7684\u5b66\u4e60\u5185\u5bb9\u3002\u6240\u6709\u64b0\u7a3f\u4eba\u5c06\u88ab\u5c55\u793a\u5728\u4ed3\u5e93\u4e0e\u7f51\u7ad9\u4e3b\u9875\uff0c\u4ee5\u611f\u8c22\u4ed6\u4eec\u5bf9\u5f00\u6e90\u793e\u533a\u7684\u65e0\u79c1\u5949\u732e\uff01</p>"},{"location":"chapter_preface/contribution/#041","title":"0.4.1. \u00a0 \u5185\u5bb9\u5fae\u8c03","text":"<p>\u6bcf\u4e2a\u9875\u9762\u7684\u53f3\u4e0a\u89d2\u90fd\u6709\u4e00\u4e2a\u300c\u7f16\u8f91\u300d\u56fe\u6807\uff0c\u4f60\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4fee\u6539\u6587\u5b57\u6216\u4ee3\u7801\uff1a</p> <ol> <li>\u70b9\u51fb\u7f16\u8f91\u6309\u94ae\uff0c\u5982\u679c\u9047\u5230\u63d0\u793a\u201c\u9700\u8981 Fork \u6b64\u4ed3\u5e93\u201d\uff0c\u8bf7\u901a\u8fc7\uff1b</li> <li>\u4fee\u6539 Markdown \u6e90\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u68c0\u67e5\u5185\u5bb9\u6b63\u786e\u6027\uff0c\u5c3d\u91cf\u4fdd\u6301\u6392\u7248\u683c\u5f0f\u7edf\u4e00\uff1b</li> <li>\u5728\u9875\u9762\u5e95\u90e8\u586b\u5199\u66f4\u6539\u8bf4\u660e\uff0c\u7136\u540e\u5355\u51fb\u201cPropose file change\u201d\u6309\u94ae\uff1b\u9875\u9762\u8df3\u8f6c\u540e\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u5373\u53ef\u3002</li> </ol> <p></p> <p> Fig. \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\u56fe\u7247\u95ee\u9898\uff0c\u6211\u4f1a\u7b2c\u4e00\u65f6\u95f4\u91cd\u65b0\u753b\u56fe\u5e76\u66ff\u6362\u56fe\u7247\u3002</p>"},{"location":"chapter_preface/contribution/#042","title":"0.4.2. \u00a0 \u5185\u5bb9\u521b\u4f5c","text":"<p>\u5982\u679c\u60a8\u60f3\u8981\u53c2\u4e0e\u672c\u5f00\u6e90\u9879\u76ee\uff0c\u5305\u62ec\u7ffb\u8bd1\u4ee3\u7801\u81f3\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u3001\u62d3\u5c55\u6587\u7ae0\u5185\u5bb9\u7b49\uff0c\u90a3\u4e48\u9700\u8981\u5b9e\u65bd Pull Request \u5de5\u4f5c\u6d41\u7a0b\uff1a</p> <ol> <li>\u767b\u5f55 GitHub \uff0c\u5e76 Fork \u672c\u4ed3\u5e93 \u81f3\u4e2a\u4eba\u8d26\u53f7\uff1b</li> <li>\u8fdb\u5165 Fork \u4ed3\u5e93\u7f51\u9875\uff0c\u4f7f\u7528 <code>git clone</code> \u514b\u9686\u8be5\u4ed3\u5e93\u81f3\u672c\u5730\uff1b</li> <li>\u5728\u672c\u5730\u8fdb\u884c\u5185\u5bb9\u521b\u4f5c\uff0c\u5e76\u901a\u8fc7\u8fd0\u884c\u6d4b\u8bd5\u6765\u9a8c\u8bc1\u4ee3\u7801\u6b63\u786e\u6027\uff1b</li> <li>\u5c06\u672c\u5730\u66f4\u6539 Commit \uff0c\u5e76 Push \u81f3\u8fdc\u7a0b\u4ed3\u5e93\uff1b</li> <li>\u5237\u65b0\u4ed3\u5e93\u7f51\u9875\uff0c\u70b9\u51fb\u201cCreate pull request\u201d\u6309\u94ae\u53d1\u8d77\u62c9\u53d6\u8bf7\u6c42\u5373\u53ef\uff1b</li> </ol>"},{"location":"chapter_preface/contribution/#043-docker","title":"0.4.3. \u00a0 Docker \u90e8\u7f72","text":"<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 Docker \u6765\u90e8\u7f72\u672c\u9879\u76ee\u3002</p> <pre><code>git clone https://github.com/krahets/hello-algo.git\ncd hello-algo\ndocker-compose up -d\n</code></pre> <p>\u7a0d\u7b49\u7247\u523b\uff0c\u5373\u53ef\u4f7f\u7528\u6d4f\u89c8\u5668\u6253\u5f00 <code>http://localhost:8000</code> \u8bbf\u95ee\u672c\u9879\u76ee\u3002</p> <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_preface/installation/","title":"0.3. \u00a0 \u7f16\u7a0b\u73af\u5883\u5b89\u88c5","text":""},{"location":"chapter_preface/installation/#031-vscode","title":"0.3.1. \u00a0 \u5b89\u88c5 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_preface/installation/#032-java","title":"0.3.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 Java Extension Pack \u3002</li> </ol>"},{"location":"chapter_preface/installation/#033-cc","title":"0.3.3. \u00a0 C/C++ \u73af\u5883","text":"<ol> <li>Windows \u7cfb\u7edf\u9700\u8981\u5b89\u88c5 MinGW\uff08\u914d\u7f6e\u6559\u7a0b\uff09\uff0cMacOS \u81ea\u5e26 Clang \u65e0\u9700\u5b89\u88c5\u3002</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>c++</code> \uff0c\u5b89\u88c5 C/C++ Extension Pack \u3002</li> </ol>"},{"location":"chapter_preface/installation/#034-python","title":"0.3.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> </ol>"},{"location":"chapter_preface/installation/#035-go","title":"0.3.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_preface/installation/#036-javascript","title":"0.3.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> </ol>"},{"location":"chapter_preface/installation/#037-c","title":"0.3.7. \u00a0 C# \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 .Net 6.0 \uff1b</li> <li>\u5728 VSCode \u7684\u63d2\u4ef6\u5e02\u573a\u4e2d\u641c\u7d22 <code>c#</code> \uff0c\u5b89\u88c5 c# \u3002</li> </ol>"},{"location":"chapter_preface/installation/#038-swift","title":"0.3.8. \u00a0 Swift \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 Swift\uff1b</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_preface/installation/#039-rust","title":"0.3.9. \u00a0 Rust \u73af\u5883","text":"<ol> <li>\u4e0b\u8f7d\u5e76\u5b89\u88c5 Rust\uff1b</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_preface/suggestions/","title":"0.2. \u00a0 \u5982\u4f55\u4f7f\u7528\u672c\u4e66","text":"<p>\u5f3a\u70c8\u5efa\u8bae\u901a\u8bfb\u672c\u8282\u5185\u5bb9\uff0c\u4ee5\u83b7\u53d6\u6700\u4f73\u5b66\u4e60\u4f53\u9a8c\u3002</p>"},{"location":"chapter_preface/suggestions/#021","title":"0.2.1. \u00a0 \u884c\u6587\u98ce\u683c\u7ea6\u5b9a","text":"<p>\u6807\u9898\u540e\u6807\u6ce8 <code>*</code> \u7684\u662f\u9009\u8bfb\u7ae0\u8282\uff0c\u5185\u5bb9\u76f8\u5bf9\u8f83\u96be\u3002\u5982\u679c\u4f60\u7684\u65f6\u95f4\u6709\u9650\uff0c\u5efa\u8bae\u53ef\u4ee5\u5148\u8df3\u8fc7\u3002</p> <p>\u6587\u7ae0\u4e2d\u7684\u91cd\u8981\u540d\u8bcd\u4f1a\u7528 <code>\u300c\u62ec\u53f7\u300d</code> \u6807\u6ce8\uff0c\u4f8b\u5982 <code>\u300c\u6570\u7ec4 Array\u300d</code> \u3002\u5efa\u8bae\u8bb0\u4f4f\u8fd9\u4e9b\u540d\u8bcd\uff0c\u5305\u62ec\u82f1\u6587\u7ffb\u8bd1\uff0c\u4ee5\u4fbf\u540e\u7eed\u9605\u8bfb\u6587\u732e\u65f6\u4f7f\u7528\u3002</p> <p>\u91cd\u70b9\u5185\u5bb9\u3001\u603b\u8d77\u53e5\u3001\u603b\u7ed3\u53e5\u4f1a\u88ab \u52a0\u7c97 \uff0c\u6b64\u7c7b\u6587\u5b57\u503c\u5f97\u7279\u522b\u5173\u6ce8\u3002</p> <p>\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</p> <p>\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</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <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>\u89c6\u9891\u548c\u56fe\u7247\u76f8\u6bd4\u4e8e\u6587\u5b57\u7684\u4fe1\u606f\u5bc6\u5ea6\u548c\u7ed3\u6784\u5316\u7a0b\u5ea6\u66f4\u9ad8\uff0c\u66f4\u5bb9\u6613\u7406\u89e3\u3002\u5728\u672c\u4e66\u4e2d\uff0c\u77e5\u8bc6\u91cd\u96be\u70b9\u4f1a\u4e3b\u8981\u4ee5\u52a8\u753b\u3001\u56fe\u89e3\u7684\u5f62\u5f0f\u5448\u73b0\uff0c\u800c\u6587\u5b57\u7684\u4f5c\u7528\u5219\u662f\u4f5c\u4e3a\u52a8\u753b\u548c\u56fe\u7684\u89e3\u91ca\u4e0e\u8865\u5145\u3002</p> <p>\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u82e5\u53d1\u73b0\u67d0\u6bb5\u5185\u5bb9\u63d0\u4f9b\u4e86\u52a8\u753b\u6216\u56fe\u89e3\uff0c\u5efa\u8bae\u4f60\u4ee5\u56fe\u4e3a\u4e3b\u7ebf\uff0c\u5c06\u6587\u5b57\u5185\u5bb9\uff08\u4e00\u822c\u5728\u56fe\u7684\u4e0a\u65b9\uff09\u5bf9\u9f50\u5230\u56fe\u4e2d\u5185\u5bb9\uff0c\u7efc\u5408\u6765\u7406\u89e3\u3002</p> <p></p> <p> Fig. \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\u6258\u7ba1\u5728GitHub \u4ed3\u5e93\uff0c\u6e90\u4ee3\u7801\u5305\u542b\u8be6\u7ec6\u6ce8\u91ca\uff0c\u914d\u6709\u6d4b\u8bd5\u6837\u4f8b\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u3002</p> <ul> <li>\u82e5\u5b66\u4e60\u65f6\u95f4\u7d27\u5f20\uff0c\u5efa\u8bae\u81f3\u5c11\u5c06\u6240\u6709\u4ee3\u7801\u901a\u8bfb\u5e76\u8fd0\u884c\u4e00\u904d\u3002</li> <li>\u82e5\u65f6\u95f4\u5141\u8bb8\uff0c\u5f3a\u70c8\u5efa\u8bae\u5bf9\u7167\u7740\u4ee3\u7801\u81ea\u5df1\u6572\u4e00\u904d\u3002\u76f8\u6bd4\u4e8e\u8bfb\u4ee3\u7801\uff0c\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u5f80\u5f80\u80fd\u5e26\u6765\u65b0\u7684\u6536\u83b7\u3002</li> </ul>"},{"location":"chapter_preface/suggestions/#1","title":"1) \u5b89\u88c5\u7f16\u7a0b\u73af\u5883","text":"<p>\u5982\u679c\u6ca1\u6709\u672c\u5730\u7f16\u7a0b\u73af\u5883\uff0c\u53ef\u4ee5\u53c2\u7167\u4e0b\u8282\u3002</p>"},{"location":"chapter_preface/suggestions/#2","title":"2) \u4e0b\u8f7d\u4ee3\u7801\u4ed3","text":"<p>\u5982\u679c\u5df2\u7ecf\u5b89\u88c5 Git \uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6765\u514b\u9686\u4ee3\u7801\u4ed3\u3002</p> <pre><code>git clone https://github.com/krahets/hello-algo.git\n</code></pre> <p>\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5\u70b9\u51fb\u201cDownload ZIP\u201d\u76f4\u63a5\u4e0b\u8f7d\u4ee3\u7801\u538b\u7f29\u5305\uff0c\u89e3\u538b\u5373\u53ef\u3002</p> <p></p> <p> Fig. \u514b\u9686\u4ed3\u5e93\u4e0e\u4e0b\u8f7d\u4ee3\u7801 </p>"},{"location":"chapter_preface/suggestions/#3","title":"3) \u8fd0\u884c\u6e90\u4ee3\u7801","text":"<p>\u82e5\u4ee3\u7801\u5757\u7684\u9876\u90e8\u6807\u6709\u6587\u4ef6\u540d\u79f0\uff0c\u5219\u53ef\u5728\u4ed3\u5e93 <code>codes</code> \u6587\u4ef6\u5939\u4e2d\u627e\u5230\u5bf9\u5e94\u7684 \u6e90\u4ee3\u7801\u6587\u4ef6\u3002</p> <p></p> <p> Fig. \u4ee3\u7801\u5757\u4e0e\u5bf9\u5e94\u7684\u6e90\u4ee3\u7801\u6587\u4ef6 </p> <p>\u6e90\u4ee3\u7801\u6587\u4ef6\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7701\u53bb\u4e0d\u5fc5\u8981\u7684\u8c03\u8bd5\u65f6\u95f4\uff0c\u5c06\u7cbe\u529b\u96c6\u4e2d\u5728\u5b66\u4e60\u5185\u5bb9\u4e0a\u3002</p> <p></p> <p> Fig. \u8fd0\u884c\u4ee3\u7801\u793a\u4f8b </p>"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4. \u00a0 \u5728\u63d0\u95ee\u8ba8\u8bba\u4e2d\u5171\u540c\u6210\u957f","text":"<p>\u9605\u8bfb\u672c\u4e66\u65f6\uff0c\u8bf7\u4e0d\u8981\u201c\u60ef\u7740\u201d\u90a3\u4e9b\u5f04\u4e0d\u660e\u767d\u7684\u77e5\u8bc6\u70b9\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u4e0b\u4f60\u7684\u95ee\u9898\uff0c\u5c0f\u4f19\u4f34\u4eec\u548c\u6211\u90fd\u4f1a\u7ed9\u4e88\u89e3\u7b54\uff0c\u60a8\u4e00\u822c 2 \u65e5\u5185\u4f1a\u5f97\u5230\u56de\u590d\u3002</p> <p>\u540c\u65f6\uff0c\u4e5f\u5e0c\u671b\u4f60\u53ef\u4ee5\u591a\u82b1\u65f6\u95f4\u901b\u901b\u8bc4\u8bba\u533a\u3002\u4e00\u65b9\u9762\uff0c\u53ef\u4ee5\u770b\u770b\u5927\u5bb6\u9047\u5230\u4e86\u4ec0\u4e48\u95ee\u9898\uff0c\u53cd\u8fc7\u6765\u67e5\u6f0f\u8865\u7f3a\uff0c\u8fd9\u5f80\u5f80\u53ef\u4ee5\u5f15\u8d77\u66f4\u52a0\u6df1\u5ea6\u7684\u601d\u8003\u3002\u53e6\u4e00\u65b9\u9762\uff0c\u4e5f\u5e0c\u671b\u4f60\u53ef\u4ee5\u6177\u6168\u5730\u89e3\u7b54\u5c0f\u4f19\u4f34\u4eec\u7684\u95ee\u9898\u3001\u5206\u4eab\u81ea\u5df1\u7684\u89c1\u89e3\uff0c\u5927\u5bb6\u4e92\u76f8\u5b66\u4e60\u4e0e\u8fdb\u6b65\uff01</p> <p></p> <p> Fig. \u8bc4\u8bba\u533a\u793a\u4f8b </p>"},{"location":"chapter_reference/","title":"\u53c2\u8003\u6587\u732e","text":"<p>[1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).</p> <p>[2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).</p> <p>[3] \u4e25\u851a\u654f. \u6570\u636e\u7ed3\u6784\uff08 C \u8bed\u8a00\u7248\uff09.</p> <p>[4] \u9093\u4fca\u8f89. \u6570\u636e\u7ed3\u6784\uff08 C++ \u8bed\u8a00\u7248\uff0c\u7b2c\u4e09\u7248\uff09.</p> <p>[5] \u9a6c\u514b\u00b7\u827e\u4f26\u00b7\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 (6th Edition). </p>"},{"location":"chapter_searching/binary_search/","title":"10.2. \u00a0 \u4e8c\u5206\u67e5\u627e","text":"<p>\u300c\u4e8c\u5206\u67e5\u627e Binary Search\u300d\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7\u6bcf\u8f6e\u7f29\u5c0f\u4e00\u534a\u641c\u7d22\u533a\u95f4\u6765\u67e5\u627e\u76ee\u6807\u5143\u7d20\u3002</p> <p>\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u6709\u4e24\u4e2a\u524d\u7f6e\u6761\u4ef6\uff1a</p> <ul> <li>\u8981\u6c42\u8f93\u5165\u6570\u636e\u662f\u6709\u5e8f\u7684\uff0c\u8fd9\u6837\u624d\u80fd\u901a\u8fc7\u5224\u65ad\u5927\u5c0f\u5173\u7cfb\u6765\u6392\u9664\u4e00\u534a\u7684\u641c\u7d22\u533a\u95f4\uff1b</li> <li>\u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\uff0c\u800c\u5728\u94fe\u8868\u4e2d\u4f7f\u7528\u6548\u7387\u5f88\u4f4e\uff0c\u56e0\u4e3a\u5176\u5728\u5faa\u73af\u4e2d\u9700\u8981\u8df3\u8dc3\u5f0f\uff08\u975e\u8fde\u7eed\u5730\uff09\u8bbf\u95ee\u5143\u7d20\u3002</li> </ul>"},{"location":"chapter_searching/binary_search/#1021","title":"10.2.1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u957f\u5ea6\u4e3a \\(n\\) \u7684\u6392\u5e8f\u6570\u7ec4 <code>nums</code> \uff0c\u5143\u7d20\u4ece\u5c0f\u5230\u5927\u6392\u5217\u3002\u6570\u7ec4\u7684\u7d22\u5f15\u53d6\u503c\u8303\u56f4\u4e3a</p> \\[ 0, 1, 2, \\cdots, n-1 \\] <p>\u4f7f\u7528\u300c\u533a\u95f4\u300d\u6765\u8868\u793a\u8fd9\u4e2a\u53d6\u503c\u8303\u56f4\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1a</p> <ol> <li>\u53cc\u95ed\u533a\u95f4 \\([0, n-1]\\) \uff0c\u5373\u4e24\u4e2a\u8fb9\u754c\u90fd\u5305\u542b\u81ea\u8eab\uff1b\u6b64\u65b9\u6cd5\u4e0b\uff0c\u533a\u95f4 \\([0, 0]\\) \u4ecd\u5305\u542b\u4e00\u4e2a\u5143\u7d20\uff1b</li> <li>\u5de6\u95ed\u53f3\u5f00 \\([0, n)\\) \uff0c\u5373\u5de6\u8fb9\u754c\u5305\u542b\u81ea\u8eab\u3001\u53f3\u8fb9\u754c\u4e0d\u5305\u542b\u81ea\u8eab\uff1b\u6b64\u65b9\u6cd5\u4e0b\uff0c\u533a\u95f4 \\([0, 0)\\) \u4e3a\u7a7a\uff1b</li> </ol>"},{"location":"chapter_searching/binary_search/#_1","title":"\u201c\u53cc\u95ed\u533a\u95f4\u201d\u5b9e\u73b0","text":"<p>\u9996\u5148\uff0c\u6211\u4eec\u5148\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u8868\u793a\uff0c\u5728\u6570\u7ec4 <code>nums</code> \u4e2d\u67e5\u627e\u76ee\u6807\u5143\u7d20 <code>target</code> \u7684\u5bf9\u5e94\u7d22\u5f15\u3002</p> <1><2><3><4><5><6><7> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p>\u4e8c\u5206\u67e5\u627e\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e0b\u7684\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_search.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = nums.length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile (i <= j) {\nint m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(vector<int>& nums, int target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = nums.size() - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile (i <= j) {\nint m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.py<pre><code>\"\"\" \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 \"\"\"\ndef binary_search(nums, target):\n# \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\ni, j = 0, len(nums) - 1\nwhile i <= j:\nm = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target: # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\nelif nums[m] > target: # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\nelse:\nreturn m # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n</code></pre> binary_search.go<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums []int, target int) int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\ni, j := 0, len(nums)-1\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nfor i <= j {\nm := (i + j) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.js<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums, target) {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nlet i = 0, j = nums.length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile (i <= j) {\nlet m = parseInt((i + j) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u5728 JS \u4e2d\u9700\u4f7f\u7528 parseInt \u51fd\u6570\u53d6\u6574\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse\nreturn m; // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.ts<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunction binarySearch(nums: number[], target: number): number {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nlet i = 0, j = nums.length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile (i <= j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n</code></pre> binary_search.c<pre><code>[class]{}-[func]{binarySearch}\n</code></pre> binary_search.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nint binarySearch(int[] nums, int target)\n{\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nint i = 0, j = nums.Length - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile (i <= j)\n{\nint m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09 */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nvar i = 0\nvar j = nums.count - 1\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile i <= j {\nlet m = (i + j) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.zig<pre><code>// \u4e8c\u5206\u67e5\u627e\uff08\u53cc\u95ed\u533a\u95f4\uff09\nfn binarySearch(comptime T: type, nums: std.ArrayList(T), target: T) T {\n// \u521d\u59cb\u5316\u53cc\u95ed\u533a\u95f4 [0, n-1] \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20\nvar i: usize = 0;\nvar j: usize = nums.items.len - 1;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i > j \u65f6\u4e3a\u7a7a\uff09\nwhile (i <= j) {\nvar m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j] \u4e2d\ni = m + 1;\n} else if (nums.items[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m-1] \u4e2d\nj = m - 1;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(T, m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre>"},{"location":"chapter_searching/binary_search/#_2","title":"\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u5b9e\u73b0","text":"<p>\u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u201c\u5de6\u95ed\u53f3\u5f00\u201d\u7684\u8868\u793a\u65b9\u6cd5\uff0c\u5199\u51fa\u76f8\u540c\u529f\u80fd\u7684\u4e8c\u5206\u67e5\u627e\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_search.java<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearch1(int[] nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\nint m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.cpp<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearch1(vector<int>& nums, int target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = nums.size();\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\nint m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.py<pre><code>\"\"\" \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 \"\"\"\ndef binary_search1(nums, target):\n# \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, j = 0, len(nums)\n# \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile i < j:\nm = (i + j) // 2 # \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target: # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\nelif nums[m] > target: # \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\nelse: # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\nreturn -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n</code></pre> binary_search.go<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearch1(nums []int, target int) int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\ni, j := 0, len(nums)\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nfor i < j {\nm := (i + j) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.js<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearch1(nums, target) {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0, j = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\nlet m = parseInt((i + j) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m \uff0c\u5728 JS \u4e2d\u9700\u4f7f\u7528 parseInt \u51fd\u6570\u53d6\u6574\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.ts<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunction binarySearch1(nums: number[], target: number): number {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nlet i = 0, j = nums.length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j) {\nconst m = Math.floor(i + (j - i) / 2); // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n}\nreturn -1; // \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n}\n</code></pre> binary_search.c<pre><code>[class]{}-[func]{binarySearch1}\n</code></pre> binary_search.cs<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nint binarySearch1(int[] nums, int target)\n{\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nint i = 0, j = nums.Length;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i < j)\n{\nint m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums[m] < target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\nelse if (nums[m] > target) // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\nelse // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> binary_search.swift<pre><code>/* \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09 */\nfunc binarySearch1(nums: [Int], target: Int) -> Int {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i = 0\nvar j = nums.count\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile i < j {\nlet m = (i + j) / 2 // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif nums[m] < target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1\n} else if nums[m] > target { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn m\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> binary_search.zig<pre><code>// \u4e8c\u5206\u67e5\u627e\uff08\u5de6\u95ed\u53f3\u5f00\uff09\nfn binarySearch1(comptime T: type, nums: std.ArrayList(T), target: T) T {\n// \u521d\u59cb\u5316\u5de6\u95ed\u53f3\u5f00 [0, n) \uff0c\u5373 i, j \u5206\u522b\u6307\u5411\u6570\u7ec4\u9996\u5143\u7d20\u3001\u5c3e\u5143\u7d20+1\nvar i: usize = 0;\nvar j: usize = nums.items.len;\n// \u5faa\u73af\uff0c\u5f53\u641c\u7d22\u533a\u95f4\u4e3a\u7a7a\u65f6\u8df3\u51fa\uff08\u5f53 i = j \u65f6\u4e3a\u7a7a\uff09\nwhile (i <= j) {\nvar m = (i + j) / 2; // \u8ba1\u7b97\u4e2d\u70b9\u7d22\u5f15 m\nif (nums.items[m] < target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [m+1, j) \u4e2d\ni = m + 1;\n} else if (nums.items[m] > target) { // \u6b64\u60c5\u51b5\u8bf4\u660e target \u5728\u533a\u95f4 [i, m) \u4e2d\nj = m;\n} else { // \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn @intCast(T, m);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre>"},{"location":"chapter_searching/binary_search/#_3","title":"\u4e24\u79cd\u8868\u793a\u5bf9\u6bd4","text":"<p>\u5bf9\u6bd4\u4e0b\u6765\uff0c\u4e24\u79cd\u8868\u793a\u7684\u4ee3\u7801\u5199\u6cd5\u6709\u4ee5\u4e0b\u4e0d\u540c\u70b9\uff1a</p> \u8868\u793a\u65b9\u6cd5 \u521d\u59cb\u5316\u6307\u9488 \u7f29\u5c0f\u533a\u95f4 \u5faa\u73af\u7ec8\u6b62\u6761\u4ef6 \u53cc\u95ed\u533a\u95f4 \\([0, n-1]\\) \\(i = 0\\) , \\(j = n-1\\) \\(i = m + 1\\) , \\(j = m - 1\\) \\(i > j\\) \u5de6\u95ed\u53f3\u5f00 \\([0, n)\\) \\(i = 0\\) , \\(j = n\\) \\(i = m + 1\\) , \\(j = m\\) \\(i = j\\) <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5728\u201c\u53cc\u95ed\u533a\u95f4\u201d\u8868\u793a\u4e2d\uff0c\u7531\u4e8e\u5bf9\u5de6\u53f3\u4e24\u8fb9\u754c\u7684\u5b9a\u4e49\u662f\u76f8\u540c\u7684\uff0c\u56e0\u6b64\u7f29\u5c0f\u533a\u95f4\u7684 \\(i\\) , \\(j\\) \u5904\u7406\u65b9\u6cd5\u4e5f\u662f\u5bf9\u79f0\u7684\uff0c\u8fd9\u6837\u66f4\u4e0d\u5bb9\u6613\u51fa\u9519\u3002\u7efc\u4e0a\u6240\u8ff0\uff0c\u5efa\u8bae\u4f60\u91c7\u7528\u201c\u53cc\u95ed\u533a\u95f4\u201d\u7684\u5199\u6cd5\u3002</p>"},{"location":"chapter_searching/binary_search/#_4","title":"\u5927\u6570\u8d8a\u754c\u5904\u7406","text":"<p>\u5f53\u6570\u7ec4\u957f\u5ea6\u5f88\u5927\u65f6\uff0c\u52a0\u6cd5 \\(i + j\\) \u7684\u7ed3\u679c\u6709\u53ef\u80fd\u4f1a\u8d85\u51fa <code>int</code> \u7c7b\u578b\u7684\u53d6\u503c\u8303\u56f4\u3002\u5728\u6b64\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u6362\u4e00\u79cd\u8ba1\u7b97\u4e2d\u70b9\u7684\u5199\u6cd5\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>// (i + j) \u6709\u53ef\u80fd\u8d85\u51fa int \u7684\u53d6\u503c\u8303\u56f4\nint m = (i + j) / 2;\n// \u66f4\u6362\u4e3a\u6b64\u5199\u6cd5\u5219\u4e0d\u4f1a\u8d8a\u754c\nint m = i + (j - i) / 2;\n</code></pre> <pre><code>// (i + j) \u6709\u53ef\u80fd\u8d85\u51fa int \u7684\u53d6\u503c\u8303\u56f4\nint m = (i + j) / 2;\n// \u66f4\u6362\u4e3a\u6b64\u5199\u6cd5\u5219\u4e0d\u4f1a\u8d8a\u754c\nint m = i + (j - i) / 2;\n</code></pre> <pre><code># Python \u4e2d\u7684\u6570\u5b57\u7406\u8bba\u4e0a\u53ef\u4ee5\u65e0\u9650\u5927\uff08\u53d6\u51b3\u4e8e\u5185\u5b58\u5927\u5c0f\uff09\n# \u56e0\u6b64\u65e0\u9700\u8003\u8651\u5927\u6570\u8d8a\u754c\u95ee\u9898\n</code></pre> <pre><code>// (i + j) \u6709\u53ef\u80fd\u8d85\u51fa int \u7684\u53d6\u503c\u8303\u56f4\nm := (i + j) / 2\n// \u66f4\u6362\u4e3a\u6b64\u5199\u6cd5\u5219\u4e0d\u4f1a\u8d8a\u754c\nm := i + (j - i) / 2\n</code></pre> <pre><code>// (i + j) \u6709\u53ef\u80fd\u8d85\u51fa int \u7684\u53d6\u503c\u8303\u56f4\nlet m = parseInt((i + j) / 2);\n// \u66f4\u6362\u4e3a\u6b64\u5199\u6cd5\u5219\u4e0d\u4f1a\u8d8a\u754c\nlet m = parseInt(i + (j - i) / 2);\n</code></pre> <pre><code>// (i + j) \u6709\u53ef\u80fd\u8d85\u51fa Number \u7684\u53d6\u503c\u8303\u56f4\nlet m = Math.floor((i + j) / 2);\n// \u66f4\u6362\u4e3a\u6b64\u5199\u6cd5\u5219\u4e0d\u4f1a\u8d8a\u754c\nlet m = Math.floor(i + (j - i) / 2);\n</code></pre> <pre><code>\n</code></pre> <pre><code>// (i + j) \u6709\u53ef\u80fd\u8d85\u51fa int \u7684\u53d6\u503c\u8303\u56f4\nint m = (i + j) / 2;\n// \u66f4\u6362\u4e3a\u6b64\u5199\u6cd5\u5219\u4e0d\u4f1a\u8d8a\u754c\nint m = i + (j - i) / 2;\n</code></pre> <pre><code>// (i + j) \u6709\u53ef\u80fd\u8d85\u51fa int \u7684\u53d6\u503c\u8303\u56f4\nlet m = (i + j) / 2\n// \u66f4\u6362\u4e3a\u6b64\u5199\u6cd5\u5219\u4e0d\u4f1a\u8d8a\u754c\nlet m = i + (j - 1) / 2\n</code></pre> <pre><code>\n</code></pre>"},{"location":"chapter_searching/binary_search/#1022","title":"10.2.2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(\\log n)\\) \uff1a\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u957f\u5ea6\uff1b\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u7684\u533a\u95f4\uff0c\u56e0\u6b64\u5faa\u73af\u8f6e\u6570\u4e3a \\(\\log_2 n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 <code>i</code> , <code>j</code> \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7a7a\u95f4\u3002</p>"},{"location":"chapter_searching/binary_search/#1023","title":"10.2.3. \u00a0 \u4f18\u70b9\u4e0e\u7f3a\u70b9","text":"<p>\u4e8c\u5206\u67e5\u627e\u6548\u7387\u5f88\u9ad8\uff0c\u4f53\u73b0\u5728\uff1a</p> <ul> <li>\u4e8c\u5206\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u4f4e\u3002\u5bf9\u6570\u9636\u5728\u6570\u636e\u91cf\u5f88\u5927\u65f6\u5177\u6709\u5de8\u5927\u4f18\u52bf\uff0c\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\u8981 \\(\\log_2 2^{20} = 20\\) \u8f6e\u5faa\u73af\u3002</li> <li>\u4e8c\u5206\u67e5\u627e\u4e0d\u9700\u8981\u989d\u5916\u7a7a\u95f4\u3002\u76f8\u5bf9\u4e8e\u501f\u52a9\u989d\u5916\u6570\u636e\u7ed3\u6784\u6765\u5b9e\u73b0\u67e5\u627e\u7684\u7b97\u6cd5\u6765\u8bf4\uff0c\u5176\u66f4\u52a0\u8282\u7ea6\u7a7a\u95f4\u4f7f\u7528\u3002</li> </ul> <p>\u4f46\u5e76\u4e0d\u610f\u5473\u7740\u6240\u6709\u60c5\u51b5\u4e0b\u90fd\u5e94\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\uff0c\u8fd9\u662f\u56e0\u4e3a\uff1a</p> <ul> <li>\u4e8c\u5206\u67e5\u627e\u4ec5\u9002\u7528\u4e8e\u6709\u5e8f\u6570\u636e\u3002\u5982\u679c\u8f93\u5165\u6570\u636e\u662f\u65e0\u5e8f\u7684\uff0c\u4e3a\u4e86\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u800c\u4e13\u95e8\u6267\u884c\u6570\u636e\u6392\u5e8f\uff0c\u90a3\u4e48\u662f\u5f97\u4e0d\u507f\u5931\u7684\uff0c\u56e0\u4e3a\u6392\u5e8f\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e00\u822c\u4e3a \\(O(n \\log n)\\) \uff0c\u6bd4\u7ebf\u6027\u67e5\u627e\u548c\u4e8c\u5206\u67e5\u627e\u90fd\u66f4\u5dee\u3002\u518d\u4f8b\u5982\uff0c\u5bf9\u4e8e\u9891\u7e41\u63d2\u5165\u5143\u7d20\u7684\u573a\u666f\uff0c\u4e3a\u4e86\u4fdd\u6301\u6570\u7ec4\u7684\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\u7531\u4e8e\u5728\u4e8c\u5206\u67e5\u627e\u4e2d\uff0c\u8bbf\u95ee\u7d22\u5f15\u662f \u201c\u975e\u8fde\u7eed\u201d \u7684\uff0c\u56e0\u6b64\u94fe\u8868\u6216\u8005\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u90fd\u65e0\u6cd5\u4f7f\u7528\u3002</li> <li>\u5728\u5c0f\u6570\u636e\u91cf\u4e0b\uff0c\u7ebf\u6027\u67e5\u627e\u7684\u6027\u80fd\u66f4\u597d\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\u5728\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/hashing_search/","title":"10.3. \u00a0 \u54c8\u5e0c\u67e5\u627e","text":"<p>Question</p> <p>\u5728\u6570\u636e\u91cf\u5f88\u5927\u65f6\uff0c\u300c\u7ebf\u6027\u67e5\u627e\u300d\u592a\u6162\uff1b\u800c\u300c\u4e8c\u5206\u67e5\u627e\u300d\u8981\u6c42\u6570\u636e\u5fc5\u987b\u662f\u6709\u5e8f\u7684\uff0c\u5e76\u4e14\u53ea\u80fd\u5728\u6570\u7ec4\u4e2d\u5e94\u7528\u3002\u90a3\u4e48\u662f\u5426\u6709\u65b9\u6cd5\u53ef\u4ee5\u540c\u65f6\u907f\u514d\u4e0a\u8ff0\u7f3a\u70b9\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u6b64\u65b9\u6cd5\u88ab\u79f0\u4e3a\u300c\u54c8\u5e0c\u67e5\u627e\u300d\u3002</p> <p>\u300c\u54c8\u5e0c\u67e5\u627e Hash Searching\u300d\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u9700\u8981\u7684\u300c\u952e\u503c\u5bf9 Key Value Pair\u300d\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 \\(O(1)\\) \u65f6\u95f4\u4e0b\u5b9e\u73b0\u201c\u952e \\(\\rightarrow\\) \u503c\u201d\u6620\u5c04\u67e5\u627e\uff0c\u4f53\u73b0\u7740\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d\u7684\u7b97\u6cd5\u601d\u60f3\u3002</p>"},{"location":"chapter_searching/hashing_search/#1031","title":"10.3.1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u76ee\u6807\u5143\u7d20 <code>target</code> \uff0c\u83b7\u53d6\u8be5\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u90a3\u4e48\u53ef\u4ee5\u501f\u52a9\u4e00\u4e2a\u54c8\u5e0c\u8868\u5b9e\u73b0\u67e5\u627e\u3002</p> <p></p> <p> Fig. \u54c8\u5e0c\u67e5\u627e\u6570\u7ec4\u7d22\u5f15 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig hashing_search.java<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nint hashingSearchArray(Map<Integer, Integer> map, int target) {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nreturn map.getOrDefault(target, -1);\n}\n</code></pre> hashing_search.cpp<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nint hashingSearchArray(unordered_map<int, int> map, int target) {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nif (map.find(target) == map.end())\nreturn -1;\nreturn map[target];\n}\n</code></pre> hashing_search.py<pre><code>\"\"\" \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 \"\"\"\ndef hashing_search_array(mapp, target):\n# \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n# \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nreturn mapp.get(target, -1)\n</code></pre> hashing_search.go<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nfunc hashingSearchArray(m map[int]int, target int) int {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nif index, ok := m[target]; ok {\nreturn index\n} else {\nreturn -1\n}\n}\n</code></pre> hashing_search.js<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nfunction hashingSearchArray(map, target) {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nreturn map.has(target) ? map.get(target) : -1;\n}\n</code></pre> hashing_search.ts<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nfunction hashingSearchArray(map: Map<number, number>, target: number): number {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nreturn map.has(target) ? map.get(target) as number : -1;\n}\n</code></pre> hashing_search.c<pre><code>[class]{}-[func]{hashingSearchArray}\n</code></pre> hashing_search.cs<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nint hashingSearchArray(Dictionary<int, int> map, int target)\n{\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nreturn map.GetValueOrDefault(target, -1);\n}\n</code></pre> hashing_search.swift<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nfunc hashingSearchArray(map: [Int: Int], target: Int) -> Int {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nreturn map[target, default: -1]\n}\n</code></pre> hashing_search.zig<pre><code>// \u54c8\u5e0c\u67e5\u627e\uff08\u6570\u7ec4\uff09\nfn hashingSearchArray(comptime T: type, map: std.AutoHashMap(T, T), target: T) T {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7d22\u5f15\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1 \nif (map.getKey(target) == null) return -1;\nreturn map.get(target).?;\n}\n</code></pre> <p>\u518d\u6bd4\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u7ed9\u5b9a\u4e00\u4e2a\u76ee\u6807\u7ed3\u70b9\u503c <code>target</code> \uff0c\u83b7\u53d6\u5bf9\u5e94\u7684\u94fe\u8868\u7ed3\u70b9\u5bf9\u8c61\uff0c\u90a3\u4e48\u4e5f\u53ef\u4ee5\u4f7f\u7528\u54c8\u5e0c\u67e5\u627e\u5b9e\u73b0\u3002</p> <p></p> <p> Fig. \u54c8\u5e0c\u67e5\u627e\u94fe\u8868\u7ed3\u70b9 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig hashing_search.java<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de null\nreturn map.getOrDefault(target, null);\n}\n</code></pre> hashing_search.cpp<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target) {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de nullptr\nif (map.find(target) == map.end())\nreturn nullptr;\nreturn map[target];\n}\n</code></pre> hashing_search.py<pre><code>\"\"\" \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 \"\"\"\ndef hashing_search_linkedlist(mapp, target):\n# \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u5143\u7d20\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n# \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de -1\nreturn mapp.get(target, -1)\n</code></pre> hashing_search.go<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nfunc hashingSearchLinkedList(m map[int]*ListNode, target int) *ListNode {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de nil\nif node, ok := m[target]; ok {\nreturn node\n} else {\nreturn nil\n}\n}\n</code></pre> hashing_search.js<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nfunction hashingSearchLinkedList(map, target) {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de null\nreturn map.has(target) ? map.get(target) : null;\n}\n</code></pre> hashing_search.ts<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nfunction hashingSearchLinkedList(map: Map<number, ListNode>, target: number): ListNode | null {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de null\nreturn map.has(target) ? map.get(target) as ListNode : null;\n}\n</code></pre> hashing_search.c<pre><code>[class]{}-[func]{hashingSearchLinkedList}\n</code></pre> hashing_search.cs<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nListNode? hashingSearchLinkedList(Dictionary<int, ListNode> map, int target)\n{\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de null\nreturn map.GetValueOrDefault(target);\n}\n</code></pre> hashing_search.swift<pre><code>/* \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nfunc hashingSearchLinkedList(map: [Int: ListNode], target: Int) -> ListNode? {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de null\nreturn map[target]\n}\n</code></pre> hashing_search.zig<pre><code>// \u54c8\u5e0c\u67e5\u627e\uff08\u94fe\u8868\uff09\nfn hashingSearchLinkedList(comptime T: type, map: std.AutoHashMap(T, *inc.ListNode(T)), target: T) ?*inc.ListNode(T) {\n// \u54c8\u5e0c\u8868\u7684 key: \u76ee\u6807\u7ed3\u70b9\u503c\uff0cvalue: \u7ed3\u70b9\u5bf9\u8c61\n// \u82e5\u54c8\u5e0c\u8868\u4e2d\u65e0\u6b64 key \uff0c\u8fd4\u56de null \nif (map.getKey(target) == null) return null;\nreturn map.get(target);\n}\n</code></pre>"},{"location":"chapter_searching/hashing_search/#1032","title":"10.3.2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u54c8\u5e0c\u8868\u7684\u67e5\u627e\u64cd\u4f5c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u957f\u5ea6\u3002</p>"},{"location":"chapter_searching/hashing_search/#1033","title":"10.3.3. \u00a0 \u4f18\u70b9\u4e0e\u7f3a\u70b9","text":"<p>\u5728\u54c8\u5e0c\u8868\u4e2d\uff0c\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \uff0c\u8fd9\u610f\u5473\u7740\u65e0\u8bba\u662f\u9ad8\u9891\u589e\u5220\u8fd8\u662f\u9ad8\u9891\u67e5\u627e\u573a\u666f\uff0c\u54c8\u5e0c\u67e5\u627e\u7684\u6027\u80fd\u8868\u73b0\u90fd\u975e\u5e38\u597d\u3002\u5f53\u7136\uff0c\u4e00\u5207\u7684\u524d\u63d0\u662f\u4fdd\u8bc1\u54c8\u5e0c\u8868\u672a\u9000\u5316\u3002</p> <p>\u5373\u4f7f\u5982\u6b64\uff0c\u54c8\u5e0c\u67e5\u627e\u4ecd\u5b58\u5728\u4e00\u4e9b\u95ee\u9898\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u60c5\u51b5\u7075\u6d3b\u9009\u62e9\u65b9\u6cd5\u3002</p> <ul> <li>\u8f85\u52a9\u54c8\u5e0c\u8868 \u9700\u8981\u4f7f\u7528 \\(O(n)\\) \u7684\u989d\u5916\u7a7a\u95f4\uff0c\u610f\u5473\u7740\u9700\u8981\u9884\u7559\u66f4\u591a\u7684\u8ba1\u7b97\u673a\u5185\u5b58\uff1b</li> <li>\u5efa\u7acb\u548c\u7ef4\u62a4\u54c8\u5e0c\u8868\u9700\u8981\u65f6\u95f4\uff0c\u56e0\u6b64\u54c8\u5e0c\u67e5\u627e \u4e0d\u9002\u5408\u9ad8\u9891\u589e\u5220\u3001\u4f4e\u9891\u67e5\u627e\u7684\u4f7f\u7528\u573a\u666f\uff1b</li> <li>\u5f53\u54c8\u5e0c\u51b2\u7a81\u4e25\u91cd\u65f6\uff0c\u54c8\u5e0c\u8868\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \uff1b</li> <li>\u5f53\u6570\u636e\u91cf\u5f88\u5c0f\u65f6\uff0c\u7ebf\u6027\u67e5\u627e\u6bd4\u54c8\u5e0c\u67e5\u627e\u66f4\u5feb\u3002\u8fd9\u662f\u56e0\u4e3a\u8ba1\u7b97\u54c8\u5e0c\u6620\u5c04\u51fd\u6570\u53ef\u80fd\u6bd4\u904d\u5386\u4e00\u4e2a\u5c0f\u578b\u6570\u7ec4\u66f4\u6162\uff1b</li> </ul>"},{"location":"chapter_searching/linear_search/","title":"10.1. \u00a0 \u7ebf\u6027\u67e5\u627e","text":"<p>\u300c\u7ebf\u6027\u67e5\u627e Linear Search\u300d\u662f\u4e00\u79cd\u6700\u57fa\u7840\u7684\u67e5\u627e\u65b9\u6cd5\uff0c\u5176\u4ece\u6570\u636e\u7ed3\u6784\u7684\u4e00\u7aef\u5f00\u59cb\uff0c\u4f9d\u6b21\u8bbf\u95ee\u6bcf\u4e2a\u5143\u7d20\uff0c\u76f4\u5230\u53e6\u4e00\u7aef\u540e\u505c\u6b62\u3002</p>"},{"location":"chapter_searching/linear_search/#1011","title":"10.1.1. \u00a0 \u7b97\u6cd5\u5b9e\u73b0","text":"<p>\u7ebf\u6027\u67e5\u627e\u5b9e\u8d28\u4e0a\u5c31\u662f\u904d\u5386\u6570\u636e\u7ed3\u6784 + \u5224\u65ad\u6761\u4ef6\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5728\u6570\u7ec4 <code>nums</code> \u4e2d\u67e5\u627e\u76ee\u6807\u5143\u7d20 <code>target</code> \u7684\u5bf9\u5e94\u7d22\u5f15\uff0c\u90a3\u4e48\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u8fdb\u884c\u7ebf\u6027\u67e5\u627e\u3002</p> <p></p> <p> Fig. \u5728\u6570\u7ec4\u4e2d\u7ebf\u6027\u67e5\u627e\u5143\u7d20 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linear_search.java<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nint linearSearchArray(int[] nums, int target) {\n// \u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.length; i++) {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nif (nums[i] == target)\nreturn i;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> linear_search.cpp<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nint linearSearchArray(vector<int>& nums, int target) {\n// \u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.size(); i++) {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nif (nums[i] == target)\nreturn i;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> linear_search.py<pre><code>\"\"\" \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09 \"\"\"\ndef linear_search_array(nums, target):\n# \u904d\u5386\u6570\u7ec4\nfor i in range(len(nums)):\nif nums[i] == target: # \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nreturn i\nreturn -1 # \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\n</code></pre> linear_search.go<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nfunc linearSearchArray(nums []int, target int) int {\n// \u904d\u5386\u6570\u7ec4\nfor i := 0; i < len(nums); i++ {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nif nums[i] == target {\nreturn i\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> linear_search.js<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nfunction linearSearchArray(nums, target) {\n// \u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nif (nums[i] === target) {\nreturn i;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1; }\n</code></pre> linear_search.ts<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09*/\nfunction linearSearchArray(nums: number[], target: number): number {\n// \u904d\u5386\u6570\u7ec4\nfor (let i = 0; i < nums.length; i++) {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nif (nums[i] === target) {\nreturn i;\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> linear_search.c<pre><code>[class]{}-[func]{linearSearchArray}\n</code></pre> linear_search.cs<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nint linearSearchArray(int[] nums, int target)\n{\n// \u904d\u5386\u6570\u7ec4\nfor (int i = 0; i < nums.Length; i++)\n{\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nif (nums[i] == target)\nreturn i;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> linear_search.swift<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09 */\nfunc linearSearchArray(nums: [Int], target: Int) -> Int {\n// \u904d\u5386\u6570\u7ec4\nfor i in nums.indices {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de\u5176\u7d22\u5f15\nif nums[i] == target {\nreturn i\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1\n}\n</code></pre> linear_search.zig<pre><code>// \u7ebf\u6027\u67e5\u627e\uff08\u6570\u7ec4\uff09\nfn linearSearchArray(comptime T: type, nums: std.ArrayList(T), target: T) T {\n// \u904d\u5386\u6570\u7ec4\nfor (nums.items) |num, i| {\n// \u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c \u8fd4\u56de\u5176\u7d22\u5f15\nif (num == target) {\nreturn @intCast(T, i);\n}\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de -1\nreturn -1;\n}\n</code></pre> <p>\u518d\u6bd4\u5982\uff0c\u6211\u4eec\u60f3\u8981\u5728\u7ed9\u5b9a\u4e00\u4e2a\u76ee\u6807\u7ed3\u70b9\u503c <code>target</code> \uff0c\u8fd4\u56de\u6b64\u7ed3\u70b9\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u5728\u94fe\u8868\u4e2d\u8fdb\u884c\u7ebf\u6027\u67e5\u627e\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linear_search.java<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nListNode linearSearchLinkedList(ListNode head, int target) {\n// \u904d\u5386\u94fe\u8868\nwhile (head != null) {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif (head.val == target)\nreturn head;\nhead = head.next;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de null\nreturn null;\n}\n</code></pre> linear_search.cpp<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nListNode* linearSearchLinkedList(ListNode* head, int target) {\n// \u904d\u5386\u94fe\u8868\nwhile (head != nullptr) {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif (head->val == target)\nreturn head;\nhead = head->next;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de nullptr\nreturn nullptr;\n}\n</code></pre> linear_search.py<pre><code>\"\"\" \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09 \"\"\"\ndef linear_search_linkedlist(head, target):\n# \u904d\u5386\u94fe\u8868\nwhile head:\nif head.val == target: # \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nreturn head\nhead = head.next\nreturn None # \u672a\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de None\n</code></pre> linear_search.go<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nfunc linearSearchLinkedList(node *ListNode, target int) *ListNode {\n// \u904d\u5386\u94fe\u8868\nfor node != nil {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif node.Val == target {\nreturn node\n}\nnode = node.Next\n}\n// \u672a\u627e\u5230\u76ee\u6807\u5143\u7d20\uff0c\u8fd4\u56de nil\nreturn nil\n}\n</code></pre> linear_search.js<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09*/\nfunction linearSearchLinkedList(head, target) {\n// \u904d\u5386\u94fe\u8868\nwhile(head) {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif(head.val === target) {\nreturn head;\n}\nhead = head.next;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de null\nreturn null;\n}\n</code></pre> linear_search.ts<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09*/\nfunction linearSearchLinkedList(head: ListNode | null, target: number): ListNode | null {\n// \u904d\u5386\u94fe\u8868\nwhile (head) {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif (head.val === target) {\nreturn head;\n}\nhead = head.next;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de null\nreturn null;\n}\n</code></pre> linear_search.c<pre><code>[class]{}-[func]{linearSearchLinkedList}\n</code></pre> linear_search.cs<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nListNode? linearSearchLinkedList(ListNode head, int target)\n{\n// \u904d\u5386\u94fe\u8868\nwhile (head != null)\n{\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif (head.val == target)\nreturn head;\nhead = head.next;\n}\n// \u672a\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de null\nreturn null;\n}\n</code></pre> linear_search.swift<pre><code>/* \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09 */\nfunc linearSearchLinkedList(head: ListNode?, target: Int) -> ListNode? {\nvar head = head\n// \u904d\u5386\u94fe\u8868\nwhile head != nil {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif head?.val == target {\nreturn head\n}\nhead = head?.next\n}\n// \u672a\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de null\nreturn nil\n}\n</code></pre> linear_search.zig<pre><code>// \u7ebf\u6027\u67e5\u627e\uff08\u94fe\u8868\uff09\nfn linearSearchLinkedList(comptime T: type, node: ?*inc.ListNode(T), target: T) ?*inc.ListNode(T) {\nvar head = node;\n// \u904d\u5386\u94fe\u8868\nwhile (head != null) {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8fd4\u56de\u4e4b\nif (head.?.val == target) return head;\nhead = head.?.next;\n}\nreturn null;\n}\n</code></pre>"},{"location":"chapter_searching/linear_search/#1012","title":"10.1.2. \u00a0 \u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u5176\u4e2d \\(n\\) \u4e3a\u6570\u7ec4\u6216\u94fe\u8868\u957f\u5ea6\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u65e0\u9700\u4f7f\u7528\u989d\u5916\u7a7a\u95f4\u3002</p>"},{"location":"chapter_searching/linear_search/#1013","title":"10.1.3. \u00a0 \u4f18\u70b9\u4e0e\u7f3a\u70b9","text":"<p>\u7ebf\u6027\u67e5\u627e\u7684\u901a\u7528\u6027\u6781\u4f73\u3002\u7531\u4e8e\u7ebf\u6027\u67e5\u627e\u662f\u4f9d\u6b21\u8bbf\u95ee\u5143\u7d20\u7684\uff0c\u5373\u6ca1\u6709\u8df3\u8dc3\u8bbf\u95ee\u5143\u7d20\uff0c\u56e0\u6b64\u6570\u7ec4\u6216\u94fe\u8868\u7686\u9002\u7528\u3002</p> <p>\u7ebf\u6027\u67e5\u627e\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u592a\u9ad8\u3002\u5728\u6570\u636e\u91cf \\(n\\) \u5f88\u5927\u65f6\uff0c\u67e5\u627e\u6548\u7387\u5f88\u4f4e\u3002</p>"},{"location":"chapter_searching/summary/","title":"10.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u7ebf\u6027\u67e5\u627e\u662f\u4e00\u79cd\u6700\u57fa\u7840\u7684\u67e5\u627e\u65b9\u6cd5\uff0c\u901a\u8fc7\u904d\u5386\u6570\u636e\u7ed3\u6784 + \u5224\u65ad\u6761\u4ef6\u5b9e\u73b0\u67e5\u627e\u3002</li> <li>\u4e8c\u5206\u67e5\u627e\u5229\u7528\u6570\u636e\u7684\u6709\u5e8f\u6027\uff0c\u901a\u8fc7\u5faa\u73af\u4e0d\u65ad\u7f29\u5c0f\u4e00\u534a\u641c\u7d22\u533a\u95f4\u6765\u5b9e\u73b0\u67e5\u627e\uff0c\u5176\u8981\u6c42\u8f93\u5165\u6570\u636e\u662f\u6709\u5e8f\u7684\uff0c\u5e76\u4e14\u4ec5\u9002\u7528\u4e8e\u6570\u7ec4\u6216\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u54c8\u5e0c\u67e5\u627e\u501f\u52a9\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u5e38\u6570\u9636\u65f6\u95f4\u590d\u6742\u5ea6\u7684\u67e5\u627e\u64cd\u4f5c\uff0c\u4f53\u73b0\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u7b97\u6cd5\u601d\u60f3\u3002</li> <li>\u4e0b\u8868\u603b\u7ed3\u5bf9\u6bd4\u4e86\u67e5\u627e\u7b97\u6cd5\u7684\u5404\u79cd\u7279\u6027\u548c\u65f6\u95f4\u590d\u6742\u5ea6\u3002</li> </ul> \u7ebf\u6027\u67e5\u627e \u4e8c\u5206\u67e5\u627e \u54c8\u5e0c\u67e5\u627e \u9002\u7528\u6570\u636e\u7ed3\u6784 \u6570\u7ec4\u3001\u94fe\u8868 \u6570\u7ec4 \u6570\u7ec4\u3001\u94fe\u8868 \u8f93\u5165\u6570\u636e\u8981\u6c42 \u65e0 \u6709\u5e8f \u65e0 \u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u67e5\u627e / \u63d2\u5165 / \u5220\u9664 \\(O(n)\\) / \\(O(1)\\) / \\(O(n)\\) \\(O(\\log n)\\) / \\(O(n)\\) / \\(O(n)\\) \\(O(1)\\) / \\(O(1)\\) / \\(O(1)\\) \u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u67e5\u627e / \u63d2\u5165 / \u5220\u9664 \\(O(n)\\) / \\(O(1)\\) / \\(O(n)\\) \\(O(\\log n)\\) / \\(O(n)\\) / \\(O(n)\\) \\(O(n)\\) / \\(O(n)\\) / \\(O(n)\\) \u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \\(O(1)\\) \\(O(n)\\)"},{"location":"chapter_sorting/bubble_sort/","title":"11.2. \u00a0 \u5192\u6ce1\u6392\u5e8f","text":"<p>\u300c\u5192\u6ce1\u6392\u5e8f Bubble Sort\u300d\u662f\u4e00\u79cd\u6700\u57fa\u7840\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u975e\u5e38\u9002\u5408\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u5b66\u4e60\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u987e\u540d\u601d\u4e49\uff0c\u300c\u5192\u6ce1\u300d\u662f\u8be5\u7b97\u6cd5\u7684\u6838\u5fc3\u64cd\u4f5c\u3002</p> <p>\u4e3a\u4ec0\u4e48\u53eb\u201c\u5192\u6ce1\u201d</p> <p>\u5728\u6c34\u4e2d\uff0c\u8d8a\u5927\u7684\u6ce1\u6ce1\u6d6e\u529b\u8d8a\u5927\uff0c\u6240\u4ee5\u6700\u5927\u7684\u6ce1\u6ce1\u4f1a\u6700\u5148\u6d6e\u5230\u6c34\u9762\u3002</p> <p>\u300c\u5192\u6ce1\u300d\u64cd\u4f5c\u5219\u662f\u5728\u6a21\u62df\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u5177\u4f53\u505a\u6cd5\u4e3a\uff1a\u4ece\u6570\u7ec4\u6700\u5de6\u7aef\u5f00\u59cb\u5411\u53f3\u904d\u5386\uff0c\u4f9d\u6b21\u5bf9\u6bd4\u76f8\u90bb\u5143\u7d20\u5927\u5c0f\uff0c\u82e5 \u5de6\u5143\u7d20 > \u53f3\u5143\u7d20 \u5219\u5c06\u5b83\u4fe9\u4ea4\u6362\uff0c\u6700\u7ec8\u53ef\u5c06\u6700\u5927\u5143\u7d20\u79fb\u52a8\u81f3\u6570\u7ec4\u6700\u53f3\u7aef\u3002</p> <p>\u5b8c\u6210\u6b64\u6b21\u5192\u6ce1\u64cd\u4f5c\u540e\uff0c\u6570\u7ec4\u6700\u5927\u5143\u7d20\u5df2\u5728\u6b63\u786e\u4f4d\u7f6e\uff0c\u63a5\u4e0b\u6765\u53ea\u9700\u6392\u5e8f\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u3002</p> <1><2><3><4><5><6><7> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p>"},{"location":"chapter_sorting/bubble_sort/#1121","title":"11.2.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<ol> <li>\u8bbe\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n\\) \uff0c\u5b8c\u6210\u7b2c\u4e00\u8f6e\u300c\u5192\u6ce1\u300d\u540e\uff0c\u6570\u7ec4\u6700\u5927\u5143\u7d20\u5df2\u5728\u6b63\u786e\u4f4d\u7f6e\uff0c\u63a5\u4e0b\u6765\u53ea\u9700\u6392\u5e8f\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u3002</li> <li>\u540c\u7406\uff0c\u5bf9\u5269\u4f59 \\(n - 1\\) \u4e2a\u5143\u7d20\u6267\u884c\u300c\u5192\u6ce1\u300d\uff0c\u53ef\u5c06\u7b2c\u4e8c\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u6b63\u786e\u4f4d\u7f6e\uff0c\u56e0\u800c\u5f85\u6392\u5e8f\u5143\u7d20\u53ea\u5269 \\(n - 2\\) \u4e2a\u3002</li> <li>\u4ee5\u6b64\u7c7b\u63a8\u2026\u2026 \u5faa\u73af \\(n - 1\\) \u8f6e\u300c\u5192\u6ce1\u300d\uff0c\u5373\u53ef\u5b8c\u6210\u6574\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\u3002</li> </ol> <p> Fig. \u5192\u6ce1\u6392\u5e8f\u6d41\u7a0b </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig bubble_sort.java<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.length - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.cpp<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(vector<int>& nums) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.size() - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\n}\n}\n}\n}\n</code></pre> bubble_sort.py<pre><code>\"\"\" \u5192\u6ce1\u6392\u5e8f \"\"\"\ndef bubble_sort(nums):\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i in range(n - 1, 0, -1):\n# \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\n</code></pre> bubble_sort.go<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i := len(nums) - 1; i > 0; i-- {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\n}\n}\n}\n}\n</code></pre> bubble_sort.js<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.ts<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunction bubbleSort(nums: number[]): void {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (let i = nums.length - 1; i > 0; i--) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.c<pre><code>[class]{}-[func]{bubbleSort}\n</code></pre> bubble_sort.cs<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nvoid bubbleSort(int[] nums)\n{\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.Length - 1; i > 0; i--)\n{\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++)\n{\nif (nums[j] > nums[j + 1])\n{\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre> bubble_sort.swift<pre><code>/* \u5192\u6ce1\u6392\u5e8f */\nfunc bubbleSort(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\n}\n}\n}\n}\n</code></pre> bubble_sort.zig<pre><code>// \u5192\u6ce1\u6392\u5e8f\nfn bubbleSort(nums: []i32) void {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nvar i: usize = nums.len - 1;\nwhile (i > 0) : (i -= 1) {\nvar j: usize = 0;\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\n}\n}\n}\n}\n</code></pre>"},{"location":"chapter_sorting/bubble_sort/#1122","title":"11.2.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n^2)\\) \uff1a\u5404\u8f6e\u300c\u5192\u6ce1\u300d\u904d\u5386\u7684\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n - 1\\) , \\(n - 2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \u6b21\uff0c\u6c42\u548c\u4e3a \\(\\frac{(n - 1) n}{2}\\) \uff0c\u56e0\u6b64\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</p> <p>\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488\u53d8\u91cf\u4ec5\u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u989d\u5916\u7a7a\u95f4\u3002</p> <p>\u7a33\u5b9a\u6392\u5e8f\uff1a\u4e0d\u4ea4\u6362\u76f8\u7b49\u5143\u7d20\u3002</p> <p>\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5f15\u5165 <code>flag</code> \u4f18\u5316\u540e\uff08\u89c1\u4e0b\u6587\uff09\uff0c\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(N)\\) \u3002</p>"},{"location":"chapter_sorting/bubble_sort/#1123","title":"11.2.3. \u00a0 \u6548\u7387\u4f18\u5316","text":"<p>\u6211\u4eec\u53d1\u73b0\uff0c\u82e5\u5728\u67d0\u8f6e\u300c\u5192\u6ce1\u300d\u4e2d\u672a\u6267\u884c\u4efb\u4f55\u4ea4\u6362\u64cd\u4f5c\uff0c\u5219\u8bf4\u660e\u6570\u7ec4\u5df2\u7ecf\u5b8c\u6210\u6392\u5e8f\uff0c\u53ef\u76f4\u63a5\u8fd4\u56de\u7ed3\u679c\u3002\u8003\u8651\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u6807\u5fd7\u4f4d <code>flag</code> \u6765\u76d1\u542c\u8be5\u60c5\u51b5\uff0c\u82e5\u51fa\u73b0\u5219\u76f4\u63a5\u8fd4\u56de\u3002</p> <p>\u4f18\u5316\u540e\uff0c\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u5dee\u548c\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a \\(O(n^2)\\) \uff1b\u800c\u5728\u8f93\u5165\u6570\u7ec4 \u5df2\u6392\u5e8f \u65f6\uff0c\u8fbe\u5230 \u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \u3002 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig bubble_sort.java<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int[] nums) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.length - 1; i > 0; i--) {\nboolean flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.cpp<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(vector<int>& nums) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.size() - 1; i > 0; i--) {\nbool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n// \u8fd9\u91cc\u4f7f\u7528\u4e86 std::swap() \u51fd\u6570\nswap(nums[j], nums[j + 1]);\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.py<pre><code>\"\"\" \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09 \"\"\"\ndef bubble_sort_with_flag(nums):\nn = len(nums)\n# \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i in range(n - 1, 0, -1):\nflag = False # \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n# \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j in range(i):\nif nums[j] > nums[j + 1]:\n# \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j + 1] = nums[j + 1], nums[j]\nflag = True # \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\nif not flag:\nbreak # \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n</code></pre> bubble_sort.go<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i := len(nums) - 1; i > 0; i-- {\nflag := false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor j := 0; j < i; j++ {\nif nums[j] > nums[j+1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nnums[j], nums[j+1] = nums[j+1], nums[j]\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif flag == false { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\n}\n}\n}\n</code></pre> bubble_sort.js<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (let i = nums.length - 1; i > 0; i--) {\nlet flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.ts<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunction bubbleSortWithFlag(nums: number[]): void {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (let i = nums.length - 1; i > 0; i--) {\nlet flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (let j = 0; j < i; j++) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.c<pre><code>[class]{}-[func]{bubbleSortWithFlag}\n</code></pre> bubble_sort.cs<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nvoid bubbleSortWithFlag(int[] nums)\n{\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor (int i = nums.Length - 1; i > 0; i--)\n{\nbool flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nfor (int j = 0; j < i; j++)\n{\nif (nums[j] > nums[j + 1])\n{\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nint tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true; // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre> bubble_sort.swift<pre><code>/* \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09*/\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i in stride(from: nums.count - 1, to: 0, by: -1) {\nvar flag = false // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nfor j in stride(from: 0, to: i, by: 1) {\nif nums[j] > nums[j + 1] {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nlet tmp = nums[j]\nnums[j] = nums[j + 1]\nnums[j + 1] = tmp\nflag = true // \u8bb0\u5f55\u4ea4\u6362\u5143\u7d20\n}\n}\nif !flag { // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\nbreak\n}\n}\n}\n</code></pre> bubble_sort.zig<pre><code>// \u5192\u6ce1\u6392\u5e8f\uff08\u6807\u5fd7\u4f18\u5316\uff09\nfn bubbleSortWithFlag(nums: []i32) void {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nvar i: usize = nums.len - 1;\nwhile (i > 0) : (i -= 1) {\nvar flag = false; // \u521d\u59cb\u5316\u6807\u5fd7\u4f4d\nvar j: usize = 0;\n// \u5185\u5faa\u73af\uff1a\u5192\u6ce1\u64cd\u4f5c\nwhile (j < i) : (j += 1) {\nif (nums[j] > nums[j + 1]) {\n// \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\nvar tmp = nums[j];\nnums[j] = nums[j + 1];\nnums[j + 1] = tmp;\nflag = true;\n}\n}\nif (!flag) break; // \u6b64\u8f6e\u5192\u6ce1\u672a\u4ea4\u6362\u4efb\u4f55\u5143\u7d20\uff0c\u76f4\u63a5\u8df3\u51fa\n}\n}\n</code></pre>"},{"location":"chapter_sorting/insertion_sort/","title":"11.3. \u00a0 \u63d2\u5165\u6392\u5e8f","text":"<p>\u300c\u63d2\u5165\u6392\u5e8f Insertion Sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e \u6570\u7ec4\u63d2\u5165\u64cd\u4f5c \u7684\u6392\u5e8f\u7b97\u6cd5\u3002</p> <p>\u300c\u63d2\u5165\u64cd\u4f5c\u300d\u539f\u7406\uff1a\u9009\u5b9a\u67d0\u4e2a\u5f85\u6392\u5e8f\u5143\u7d20\u4e3a\u57fa\u51c6\u6570 <code>base</code>\uff0c\u5c06 <code>base</code> \u4e0e\u5176\u5de6\u4fa7\u5df2\u6392\u5e8f\u533a\u95f4\u5143\u7d20\u4f9d\u6b21\u5bf9\u6bd4\u5927\u5c0f\uff0c\u5e76\u63d2\u5165\u5230\u6b63\u786e\u4f4d\u7f6e\u3002</p> <p>\u56de\u5fc6\u6570\u7ec4\u63d2\u5165\u64cd\u4f5c\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> Fig. \u5355\u6b21\u63d2\u5165\u64cd\u4f5c </p>"},{"location":"chapter_sorting/insertion_sort/#1131","title":"11.3.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<ol> <li>\u7b2c 1 \u8f6e\u5148\u9009\u53d6\u6570\u7ec4\u7684 \u7b2c 2 \u4e2a\u5143\u7d20 \u4e3a <code>base</code> \uff0c\u6267\u884c\u300c\u63d2\u5165\u64cd\u4f5c\u300d\u540e\uff0c\u6570\u7ec4\u524d 2 \u4e2a\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002</li> <li>\u7b2c 2 \u8f6e\u9009\u53d6 \u7b2c 3 \u4e2a\u5143\u7d20 \u4e3a <code>base</code> \uff0c\u6267\u884c\u300c\u63d2\u5165\u64cd\u4f5c\u300d\u540e\uff0c\u6570\u7ec4\u524d 3 \u4e2a\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002</li> <li>\u4ee5\u6b64\u7c7b\u63a8\u2026\u2026\u6700\u540e\u4e00\u8f6e\u9009\u53d6 \u6570\u7ec4\u5c3e\u5143\u7d20 \u4e3a <code>base</code> \uff0c\u6267\u884c\u300c\u63d2\u5165\u64cd\u4f5c\u300d\u540e\uff0c\u6240\u6709\u5143\u7d20\u5df2\u5b8c\u6210\u6392\u5e8f\u3002</li> </ol> <p> Fig. \u63d2\u5165\u6392\u5e8f\u6d41\u7a0b </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig insertion_sort.java<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums) {\n// \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nfor (int i = 1; i < nums.length; i++) {\nint base = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.cpp<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(vector<int>& nums) {\n// \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nfor (int i = 1; i < nums.size(); i++) {\nint base = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.py<pre><code>\"\"\" \u63d2\u5165\u6392\u5e8f \"\"\"\ndef insertion_sort(nums):\n# \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nfor i in range(1, len(nums)):\nbase = nums[i]\nj = i - 1\n# \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j >= 0 and nums[j] > base:\nnums[j + 1] = nums[j] # 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\nnums[j + 1] = base # 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n</code></pre> insertion_sort.go<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums []int) {\n// \u5916\u5faa\u73af\uff1a\u5f85\u6392\u5e8f\u5143\u7d20\u6570\u91cf\u4e3a n-1, n-2, ..., 1\nfor i := 1; i < len(nums); i++ {\nbase := nums[i]\nj := i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nfor j >= 0 && nums[j] > base {\nnums[j+1] = nums[j] // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--\n}\nnums[j+1] = base // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.js<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums) {\n// \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nfor (let i = 1; i < nums.length; i++) {\nlet base = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.ts<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunction insertionSort(nums: number[]): void {\n// \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nfor (let i = 1; i < nums.length; i++) {\nconst base = nums[i];\nlet j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > base) {\nnums[j + 1] = nums[j]; // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = base; // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.c<pre><code>[class]{}-[func]{insertionSort}\n</code></pre> insertion_sort.cs<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nvoid insertionSort(int[] nums)\n{\n// \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nfor (int i = 1; i < nums.Length; i++)\n{\nint bas = nums[i], j = i - 1;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 0 && nums[j] > bas)\n{\nnums[j + 1] = nums[j]; // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj--;\n}\nnums[j + 1] = bas; // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.swift<pre><code>/* \u63d2\u5165\u6392\u5e8f */\nfunc insertionSort(nums: inout [Int]) {\n// \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nfor i in stride(from: 1, to: nums.count, by: 1) {\nlet base = nums[i]\nvar j = i - 1\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile j >= 0, nums[j] > base {\nnums[j + 1] = nums[j] // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\nj -= 1\n}\nnums[j + 1] = base // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre> insertion_sort.zig<pre><code>// \u63d2\u5165\u6392\u5e8f\nfn insertionSort(nums: []i32) void {\n// \u5916\u5faa\u73af\uff1abase = nums[1], nums[2], ..., nums[n-1]\nvar i: usize = 1;\nwhile (i < nums.len) : (i += 1) {\nvar base = nums[i];\nvar j: usize = i;\n// \u5185\u5faa\u73af\uff1a\u5c06 base \u63d2\u5165\u5230\u5de6\u8fb9\u7684\u6b63\u786e\u4f4d\u7f6e\nwhile (j >= 1 and nums[j - 1] > base) : (j -= 1) {\nnums[j] = nums[j - 1]; // 1. \u5c06 nums[j] \u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n}\nnums[j] = base; // 2. \u5c06 base \u8d4b\u503c\u5230\u6b63\u786e\u4f4d\u7f6e\n}\n}\n</code></pre>"},{"location":"chapter_sorting/insertion_sort/#1132","title":"11.3.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n^2)\\) \uff1a\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u5404\u8f6e\u63d2\u5165\u64cd\u4f5c\u5faa\u73af \\(n - 1\\) , \\(n-2\\) , \\(\\cdots\\) , \\(2\\) , \\(1\\) \u6b21\uff0c\u6c42\u548c\u4e3a \\(\\frac{(n - 1) n}{2}\\) \uff0c\u4f7f\u7528 \\(O(n^2)\\) \u65f6\u95f4\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(1)\\) \uff1a\u6307\u9488 \\(i\\) , \\(j\\) \u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\u3002</p> <p>\u539f\u5730\u6392\u5e8f\uff1a\u6307\u9488\u53d8\u91cf\u4ec5\u4f7f\u7528\u5e38\u6570\u5927\u5c0f\u989d\u5916\u7a7a\u95f4\u3002</p> <p>\u7a33\u5b9a\u6392\u5e8f\uff1a\u4e0d\u4ea4\u6362\u76f8\u7b49\u5143\u7d20\u3002</p> <p>\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u6700\u4f73\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n)\\) \u3002</p>"},{"location":"chapter_sorting/insertion_sort/#1133-vs","title":"11.3.3. \u00a0 \u63d2\u5165\u6392\u5e8f vs \u5192\u6ce1\u6392\u5e8f","text":"<p>Question</p> <p>\u867d\u7136\u300c\u63d2\u5165\u6392\u5e8f\u300d\u548c\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(n^2)\\) \uff0c\u4f46\u5b9e\u9645\u8fd0\u884c\u901f\u5ea6\u5374\u6709\u5f88\u5927\u5dee\u522b\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f</p> <p>\u56de\u987e\u590d\u6742\u5ea6\u5206\u6790\uff0c\u4e24\u4e2a\u65b9\u6cd5\u7684\u5faa\u73af\u6b21\u6570\u90fd\u662f \\(\\frac{(n - 1) n}{2}\\) \u3002\u4f46\u4e0d\u540c\u7684\u662f\uff0c\u300c\u5192\u6ce1\u64cd\u4f5c\u300d\u662f\u5728\u505a \u5143\u7d20\u4ea4\u6362\uff0c\u9700\u8981\u501f\u52a9\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u5b9e\u73b0\uff0c\u5171 3 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u800c\u300c\u63d2\u5165\u64cd\u4f5c\u300d\u662f\u5728\u505a \u8d4b\u503c\uff0c\u53ea\u9700 1 \u4e2a\u5355\u5143\u64cd\u4f5c\uff1b\u56e0\u6b64\uff0c\u53ef\u4ee5\u7c97\u7565\u4f30\u8ba1\u51fa\u5192\u6ce1\u6392\u5e8f\u7684\u8ba1\u7b97\u5f00\u9500\u7ea6\u4e3a\u63d2\u5165\u6392\u5e8f\u7684 3 \u500d\u3002</p> <p>\u63d2\u5165\u6392\u5e8f\u8fd0\u884c\u901f\u5ea6\u5feb\uff0c\u5e76\u4e14\u5177\u6709\u539f\u5730\u3001\u7a33\u5b9a\u3001\u81ea\u9002\u5e94\u7684\u4f18\u70b9\uff0c\u56e0\u6b64\u5f88\u53d7\u6b22\u8fce\u3002\u5b9e\u9645\u4e0a\uff0c\u5305\u62ec Java \u5728\u5185\u7684\u8bb8\u591a\u7f16\u7a0b\u8bed\u8a00\u7684\u6392\u5e8f\u5e93\u51fd\u6570\u7684\u5b9e\u73b0\u90fd\u7528\u5230\u4e86\u63d2\u5165\u6392\u5e8f\u3002\u5e93\u51fd\u6570\u7684\u5927\u81f4\u601d\u8def\uff1a</p> <ul> <li>\u5bf9\u4e8e \u957f\u6570\u7ec4\uff0c\u91c7\u7528\u57fa\u4e8e\u5206\u6cbb\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u4f8b\u5982\u300c\u5feb\u901f\u6392\u5e8f\u300d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n \\log n)\\) \uff1b</li> <li>\u5bf9\u4e8e \u77ed\u6570\u7ec4\uff0c\u76f4\u63a5\u4f7f\u7528\u300c\u63d2\u5165\u6392\u5e8f\u300d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff1b</li> </ul> <p>\u5728\u6570\u7ec4\u8f83\u77ed\u65f6\uff0c\u590d\u6742\u5ea6\u4e2d\u7684\u5e38\u6570\u9879\uff08\u5373\u6bcf\u8f6e\u4e2d\u7684\u5355\u5143\u64cd\u4f5c\u6570\u91cf\uff09\u5360\u4e3b\u5bfc\u4f5c\u7528\uff0c\u6b64\u65f6\u63d2\u5165\u6392\u5e8f\u8fd0\u884c\u5730\u66f4\u5feb\u3002\u8fd9\u4e2a\u73b0\u8c61\u4e0e\u300c\u7ebf\u6027\u67e5\u627e\u300d\u548c\u300c\u4e8c\u5206\u67e5\u627e\u300d\u7684\u60c5\u51b5\u7c7b\u4f3c\u3002</p>"},{"location":"chapter_sorting/intro_to_sort/","title":"11.1. \u00a0 \u6392\u5e8f\u7b80\u4ecb","text":"<p>\u300c\u6392\u5e8f\u7b97\u6cd5 Sorting Algorithm\u300d\u4f7f\u5f97\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u6309\u7167\u4ece\u5c0f\u5230\u5927\u7684\u987a\u5e8f\u6392\u5217\u3002</p> <ul> <li>\u5f85\u6392\u5e8f\u7684\u5217\u8868\u7684 \u5143\u7d20\u7c7b\u578b \u53ef\u4ee5\u662f\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5b57\u7b26\u3001\u6216\u5b57\u7b26\u4e32\uff1b</li> <li>\u6392\u5e8f\u7b97\u6cd5\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8bbe\u5b9a \u5224\u65ad\u89c4\u5219\uff0c\u4f8b\u5982\u6570\u5b57\u5927\u5c0f\u3001\u5b57\u7b26 ASCII \u7801\u987a\u5e8f\u3001\u81ea\u5b9a\u4e49\u89c4\u5219\uff1b</li> </ul> <p></p> <p> Fig. \u6392\u5e8f\u4e2d\u4e0d\u540c\u7684\u5143\u7d20\u7c7b\u578b\u548c\u5224\u65ad\u89c4\u5219 </p>"},{"location":"chapter_sorting/intro_to_sort/#1111","title":"11.1.1. \u00a0 \u8bc4\u4ef7\u7ef4\u5ea6","text":"<p>\u6392\u5e8f\u7b97\u6cd5\u4e3b\u8981\u53ef\u6839\u636e \u7a33\u5b9a\u6027 \u3001\u5c31\u5730\u6027 \u3001\u81ea\u9002\u5e94\u6027 \u3001\u6bd4\u8f83\u7c7b \u6765\u5206\u7c7b\u3002</p>"},{"location":"chapter_sorting/intro_to_sort/#_1","title":"\u7a33\u5b9a\u6027","text":"<ul> <li>\u300c\u7a33\u5b9a\u6392\u5e8f\u300d\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u4e0d\u6539\u53d8 \u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u987a\u5e8f\u3002</li> <li>\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u5728\u5b8c\u6210\u6392\u5e8f\u540e\uff0c\u76f8\u7b49\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u76f8\u5bf9\u4f4d\u7f6e \u53ef\u80fd\u88ab\u6539\u53d8\u3002</li> </ul> <p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u7b2c 1, 2 \u5217\u5206\u522b\u662f\u59d3\u540d\u548c\u5e74\u9f84\u3002\u90a3\u4e48\u5728\u4ee5\u4e0b\u793a\u4f8b\u4e2d\uff0c\u300c\u975e\u7a33\u5b9a\u6392\u5e8f\u300d\u4f1a\u5bfc\u81f4\u8f93\u5165\u6570\u636e\u7684\u6709\u5e8f\u6027\u4e22\u5931\u3002\u56e0\u6b64\u300c\u7a33\u5b9a\u6392\u5e8f\u300d\u662f\u5f88\u597d\u7684\u7279\u6027\uff0c\u5728\u591a\u7ea7\u6392\u5e8f\u4e2d\u662f\u5fc5\u987b\u7684\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>"},{"location":"chapter_sorting/intro_to_sort/#_2","title":"\u5c31\u5730\u6027","text":"<ul> <li>\u300c\u539f\u5730\u6392\u5e8f\u300d\u65e0\u9700\u8f85\u52a9\u6570\u636e\uff0c\u4e0d\u4f7f\u7528\u989d\u5916\u7a7a\u95f4\uff1b</li> <li>\u300c\u975e\u539f\u5730\u6392\u5e8f\u300d\u9700\u8981\u501f\u52a9\u8f85\u52a9\u6570\u636e\uff0c\u4f7f\u7528\u989d\u5916\u7a7a\u95f4\uff1b</li> </ul> <p>\u300c\u539f\u5730\u6392\u5e8f\u300d\u4e0d\u4f7f\u7528\u989d\u5916\u7a7a\u95f4\uff0c\u53ef\u4ee5\u8282\u7ea6\u5185\u5b58\uff1b\u5e76\u4e14\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7531\u4e8e\u6570\u636e\u64cd\u4f5c\u51cf\u5c11\uff0c\u539f\u5730\u6392\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u4e5f\u66f4\u9ad8\u3002</p>"},{"location":"chapter_sorting/intro_to_sort/#_3","title":"\u81ea\u9002\u5e94\u6027","text":"<ul> <li>\u300c\u81ea\u9002\u5e94\u6392\u5e8f\u300d\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53d7\u8f93\u5165\u6570\u636e\u5f71\u54cd\uff0c\u5373\u6700\u4f73 / \u6700\u5dee / \u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e0d\u76f8\u7b49\u3002</li> <li>\u300c\u975e\u81ea\u9002\u5e94\u6392\u5e8f\u300d\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6052\u5b9a\uff0c\u4e0e\u8f93\u5165\u6570\u636e\u65e0\u5173\u3002</li> </ul> <p>\u6211\u4eec\u5e0c\u671b \u6700\u5dee = \u5e73\u5747\uff0c\u5373\u4e0d\u5e0c\u671b\u6392\u5e8f\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u5728\u67d0\u4e9b\u8f93\u5165\u6570\u636e\u4e0b\u53d1\u751f\u52a3\u5316\u3002</p>"},{"location":"chapter_sorting/intro_to_sort/#_4","title":"\u6bd4\u8f83\u7c7b","text":"<ul> <li>\u300c\u6bd4\u8f83\u7c7b\u6392\u5e8f\u300d\u57fa\u4e8e\u5143\u7d20\u4e4b\u95f4\u7684\u6bd4\u8f83\u7b97\u5b50\uff08\u5c0f\u4e8e\u3001\u76f8\u7b49\u3001\u5927\u4e8e\uff09\u6765\u51b3\u5b9a\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u3002</li> <li>\u300c\u975e\u6bd4\u8f83\u7c7b\u6392\u5e8f\u300d\u4e0d\u57fa\u4e8e\u5143\u7d20\u4e4b\u95f4\u7684\u6bd4\u8f83\u7b97\u5b50\u6765\u51b3\u5b9a\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u3002</li> </ul> <p>\u300c\u6bd4\u8f83\u7c7b\u6392\u5e8f\u300d\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6700\u4f18\u4e3a \\(O(n \\log n)\\) \uff1b\u800c\u300c\u975e\u6bd4\u8f83\u7c7b\u6392\u5e8f\u300d\u53ef\u4ee5\u8fbe\u5230 \\(O(n)\\) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u4f46\u901a\u7528\u6027\u8f83\u5dee\u3002</p>"},{"location":"chapter_sorting/intro_to_sort/#1112","title":"11.1.2. \u00a0 \u7406\u60f3\u6392\u5e8f\u7b97\u6cd5","text":"<ul> <li>\u8fd0\u884c\u5feb\uff0c\u5373\u65f6\u95f4\u590d\u6742\u5ea6\u4f4e\uff1b</li> <li>\u7a33\u5b9a\u6392\u5e8f\uff0c\u5373\u6392\u5e8f\u540e\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u53d8\u5316\uff1b</li> <li>\u539f\u5730\u6392\u5e8f\uff0c\u5373\u8fd0\u884c\u4e2d\u4e0d\u4f7f\u7528\u989d\u5916\u7684\u8f85\u52a9\u7a7a\u95f4\uff1b</li> <li>\u6b63\u5411\u81ea\u9002\u5e94\u6027\uff0c\u5373\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u4e0d\u4f1a\u5728\u67d0\u4e9b\u8f93\u5165\u6570\u636e\u4e0b\u53d1\u751f\u52a3\u5316\uff1b</li> </ul> <p>\u7136\u800c\uff0c\u6ca1\u6709\u6392\u5e8f\u7b97\u6cd5\u540c\u65f6\u5177\u5907\u4ee5\u4e0a\u6240\u6709\u7279\u6027\u3002\u6392\u5e8f\u7b97\u6cd5\u7684\u9009\u578b\u4f7f\u7528\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5217\u8868\u7c7b\u578b\u3001\u5217\u8868\u957f\u5ea6\u3001\u5143\u7d20\u5206\u5e03\u7b49\u56e0\u7d20\u3002</p>"},{"location":"chapter_sorting/merge_sort/","title":"11.5. \u00a0 \u5f52\u5e76\u6392\u5e8f","text":"<p>\u300c\u5f52\u5e76\u6392\u5e8f Merge Sort\u300d\u662f\u7b97\u6cd5\u4e2d\u201c\u5206\u6cbb\u601d\u60f3\u201d\u7684\u5178\u578b\u4f53\u73b0\uff0c\u5176\u6709\u300c\u5212\u5206\u300d\u548c\u300c\u5408\u5e76\u300d\u4e24\u4e2a\u9636\u6bb5\uff1a</p> <ol> <li>\u5212\u5206\u9636\u6bb5\uff1a\u901a\u8fc7\u9012\u5f52\u4e0d\u65ad \u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u4f4d\u7f6e\u5212\u5206\u5f00\uff0c\u5c06\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u8f6c\u5316\u4e3a\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\uff1b</li> <li>\u5408\u5e76\u9636\u6bb5\uff1a\u5212\u5206\u5230\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\uff0c\u5f00\u59cb\u5411\u4e0a\u5408\u5e76\uff0c\u4e0d\u65ad\u5c06 \u5de6\u3001\u53f3\u4e24\u4e2a\u77ed\u6392\u5e8f\u6570\u7ec4 \u5408\u5e76\u4e3a \u4e00\u4e2a\u957f\u6392\u5e8f\u6570\u7ec4\uff0c\u76f4\u81f3\u5408\u5e76\u81f3\u539f\u6570\u7ec4\u65f6\u5b8c\u6210\u6392\u5e8f\uff1b</li> </ol> <p></p> <p> Fig. \u5f52\u5e76\u6392\u5e8f\u7684\u5212\u5206\u4e0e\u5408\u5e76\u9636\u6bb5 </p>"},{"location":"chapter_sorting/merge_sort/#1151","title":"11.5.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<p>\u300c\u9012\u5f52\u5212\u5206\u300d \u4ece\u9876\u81f3\u5e95\u9012\u5f52\u5730 \u5c06\u6570\u7ec4\u4ece\u4e2d\u70b9\u5207\u4e3a\u4e24\u4e2a\u5b50\u6570\u7ec4\uff0c\u76f4\u81f3\u957f\u5ea6\u4e3a 1 \uff1b</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\uff1b</li> <li>\u9012\u5f52\u6267\u884c <code>1.</code> \u6b65\u9aa4\uff0c\u76f4\u81f3\u5b50\u6570\u7ec4\u533a\u95f4\u957f\u5ea6\u4e3a 1 \u65f6\uff0c\u7ec8\u6b62\u9012\u5f52\u5212\u5206\uff1b</li> </ol> <p>\u300c\u56de\u6eaf\u5408\u5e76\u300d \u4ece\u5e95\u81f3\u9876\u5730\u5c06\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a \u6709\u5e8f\u6570\u7ec4 \uff1b</p> <p>\u9700\u8981\u6ce8\u610f\uff0c\u7531\u4e8e\u4ece\u957f\u5ea6\u4e3a 1 \u7684\u5b50\u6570\u7ec4\u5f00\u59cb\u5408\u5e76\uff0c\u6240\u4ee5 \u6bcf\u4e2a\u5b50\u6570\u7ec4\u90fd\u662f\u6709\u5e8f\u7684\u3002\u56e0\u6b64\uff0c\u5408\u5e76\u4efb\u52a1\u672c\u8d28\u662f\u8981 \u5c06\u4e24\u4e2a\u6709\u5e8f\u5b50\u6570\u7ec4\u5408\u5e76\u4e3a\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\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>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5f52\u5e76\u6392\u5e8f\u7684\u9012\u5f52\u987a\u5e8f\u5c31\u662f\u4e8c\u53c9\u6811\u7684\u300c\u540e\u5e8f\u904d\u5386\u300d\u3002</p> <ul> <li>\u540e\u5e8f\u904d\u5386\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6811\u3001\u518d\u9012\u5f52\u53f3\u5b50\u6811\u3001\u6700\u540e\u5904\u7406\u6839\u7ed3\u70b9\u3002</li> <li>\u5f52\u5e76\u6392\u5e8f\uff1a\u5148\u9012\u5f52\u5de6\u5b50\u6811\u3001\u518d\u9012\u5f52\u53f3\u5b50\u6811\u3001\u6700\u540e\u5904\u7406\u5408\u5e76\u3002</li> </ul> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig merge_sort.java<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(int[] nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = Arrays.copyOfRange(nums, left, right + 1); // \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart; // \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i > leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.cpp<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(vector<int>& nums, int left, int mid, int right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nvector<int> tmp(nums.begin() + left, nums.begin() + right + 1); // \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart; // \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i > leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(vector<int>& nums, int left, int right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.py<pre><code>\"\"\" \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 \"\"\"\n# \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n# \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\ndef merge(nums, left, mid, right):\n# \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4 \u501f\u52a9 copy\u6a21\u5757\ntmp = nums[left:right + 1]\n# \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleft_start, left_end = left - left, mid - left\n# \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nright_start, right_end = mid + 1 - left, right - left\n# i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni, j = left_start, right_start\n# \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in range(left, right + 1):\n# \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif i > left_end:\nnums[k] = tmp[j]\nj += 1\n# \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelif j > right_end or tmp[i] <= tmp[j]:\nnums[k] = tmp[i]\ni += 1\n# \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse:\nnums[k] = tmp[j]\nj += 1\n\"\"\" \u5f52\u5e76\u6392\u5e8f \"\"\"\ndef merge_sort(nums, left, right):\n# \u7ec8\u6b62\u6761\u4ef6\nif left >= right:\nreturn # \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n# \u5212\u5206\u9636\u6bb5\nmid = (left + right) // 2 # \u8ba1\u7b97\u4e2d\u70b9\nmerge_sort(nums, left, mid) # \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmerge_sort(nums, mid + 1, right) # \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n# \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n</code></pre> merge_sort.go<pre><code>/*\n \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\n \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\n*/\nfunc merge(nums []int, left, mid, right int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4 \u501f\u52a9 copy \u6a21\u5757\ntmp := make([]int, right-left+1)\nfor i := left; i <= right; i++ {\ntmp[i-left] = nums[i]\n}\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nleftStart, leftEnd := left-left, mid-left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nrightStart, rightEnd := mid+1-left, right-left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\ni, j := leftStart, rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k := left; k <= right; k++ {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif i > leftEnd {\nnums[k] = tmp[j]\nj++\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n} else if j > rightEnd || tmp[i] <= tmp[j] {\nnums[k] = tmp[i]\ni++\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n} else {\nnums[k] = tmp[j]\nj++\n}\n}\n}\nfunc mergeSort(nums []int, left, right int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left >= right {\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nmid := (left + right) / 2\nmergeSort(nums, left, mid)\nmergeSort(nums, mid+1, right)\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right)\n}\n</code></pre> merge_sort.js<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfunction merge(nums, left, mid, right) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1); // \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nlet leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nlet rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart, j = rightStart; // \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k <= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i > leftEnd) {\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n} else if (j > rightEnd || tmp[i] <= tmp[j]) {\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n} else {\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums, left, right) {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.ts<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = nums.slice(left, right + 1);\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nlet i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (let k = left; k <= right; k++) {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i > leftEnd) {\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\n} else if (j > rightEnd || tmp[i] <= tmp[j]) {\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\n} else {\nnums[k] = tmp[j++];\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nlet mid = Math.floor((left + right) / 2); // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.c<pre><code>\n</code></pre> merge_sort.cs<pre><code>/* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4 */\n// \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n// \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\nvoid merge(int[] nums, int left, int mid, int right)\n{\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nint[] tmp = nums[left..(right + 1)];\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint leftStart = left - left, leftEnd = mid - left;\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15 \nint rightStart = mid + 1 - left, rightEnd = right - left;\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nint i = leftStart, j = rightStart;\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor (int k = left; k <= right; k++)\n{\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif (i > leftEnd)\nnums[k] = tmp[j++];\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if (j > rightEnd || tmp[i] <= tmp[j])\nnums[k] = tmp[i++];\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse\nnums[k] = tmp[j++];\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nvoid mergeSort(int[] nums, int left, int right)\n{\n// \u7ec8\u6b62\u6761\u4ef6\nif (left >= right) return; // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\n// \u5212\u5206\u9636\u6bb5\nint mid = (left + right) / 2; // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums, left, mid); // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums, mid + 1, right); // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums, left, mid, right);\n}\n</code></pre> merge_sort.swift<pre><code>/**\n* \u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\n* \u5de6\u5b50\u6570\u7ec4\u533a\u95f4 [left, mid]\n* \u53f3\u5b50\u6570\u7ec4\u533a\u95f4 [mid + 1, right]\n*/\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n// \u521d\u59cb\u5316\u8f85\u52a9\u6570\u7ec4\nlet tmp = Array(nums[left ..< (right + 1)])\n// \u5de6\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet leftStart = left - left\nlet leftEnd = mid - left\n// \u53f3\u5b50\u6570\u7ec4\u7684\u8d77\u59cb\u7d22\u5f15\u548c\u7ed3\u675f\u7d22\u5f15\nlet rightStart = mid + 1 - left\nlet rightEnd = right - left\n// i, j \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u7684\u9996\u5143\u7d20\nvar i = leftStart\nvar j = rightStart\n// \u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 nums \u6765\u5408\u5e76\u5de6\u5b50\u6570\u7ec4\u548c\u53f3\u5b50\u6570\u7ec4\nfor k in left ... right {\n// \u82e5\u201c\u5de6\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nif i > leftEnd {\nnums[k] = tmp[j]\nj += 1\n}\n// \u5426\u5219\uff0c\u82e5\u201c\u53f3\u5b50\u6570\u7ec4\u5df2\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u6216\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 <= \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u5de6\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 i++\nelse if j > rightEnd || tmp[i] <= tmp[j] {\nnums[k] = tmp[i]\ni += 1\n}\n// \u5426\u5219\uff0c\u82e5\u201c\u5de6\u53f3\u5b50\u6570\u7ec4\u90fd\u672a\u5168\u90e8\u5408\u5e76\u5b8c\u201d\u4e14\u201c\u5de6\u5b50\u6570\u7ec4\u5143\u7d20 > \u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u201d\uff0c\u5219\u9009\u53d6\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\uff0c\u5e76\u4e14 j++\nelse {\nnums[k] = tmp[j]\nj += 1\n}\n}\n}\n/* \u5f52\u5e76\u6392\u5e8f */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n// \u7ec8\u6b62\u6761\u4ef6\nif left >= right { // \u5f53\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nreturn\n}\n// \u5212\u5206\u9636\u6bb5\nlet mid = (left + right) / 2 // \u8ba1\u7b97\u4e2d\u70b9\nmergeSort(nums: &nums, left: left, right: mid) // \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\nmergeSort(nums: &nums, left: mid + 1, right: right) // \u9012\u5f52\u53f3\u5b50\u6570\u7ec4\n// \u5408\u5e76\u9636\u6bb5\nmerge(nums: &nums, left: left, mid: mid, right: right)\n}\n</code></pre> merge_sort.zig<pre><code>\n</code></pre> <p>\u4e0b\u9762\u91cd\u70b9\u89e3\u91ca\u4e00\u4e0b\u5408\u5e76\u65b9\u6cd5 <code>merge()</code> \u7684\u6d41\u7a0b\uff1a</p> <ol> <li>\u521d\u59cb\u5316\u4e00\u4e2a\u8f85\u52a9\u6570\u7ec4 <code>tmp</code> \u6682\u5b58\u5f85\u5408\u5e76\u533a\u95f4 <code>[left, right]</code> \u5185\u7684\u5143\u7d20\uff0c\u540e\u7eed\u901a\u8fc7\u8986\u76d6\u539f\u6570\u7ec4 <code>nums</code> \u7684\u5143\u7d20\u6765\u5b9e\u73b0\u5408\u5e76\uff1b</li> <li>\u521d\u59cb\u5316\u6307\u9488 <code>i</code> , <code>j</code> , <code>k</code> \u5206\u522b\u6307\u5411\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\u3001\u539f\u6570\u7ec4\u7684\u9996\u5143\u7d20\uff1b</li> <li>\u5faa\u73af\u5224\u65ad <code>tmp[i]</code> \u548c <code>tmp[j]</code> \u7684\u5927\u5c0f\uff0c\u5c06\u8f83\u5c0f\u7684\u5148\u8986\u76d6\u81f3 <code>nums[k]</code> \uff0c\u6307\u9488 <code>i</code> , <code>j</code> \u6839\u636e\u5224\u65ad\u7ed3\u679c\u4ea4\u66ff\u524d\u8fdb\uff08\u6307\u9488 <code>k</code> \u4e5f\u524d\u8fdb\uff09\uff0c\u76f4\u81f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u90fd\u904d\u5386\u5b8c\uff0c\u5373\u53ef\u5b8c\u6210\u5408\u5e76\u3002</li> </ol> <p>\u5408\u5e76\u65b9\u6cd5 <code>merge()</code> \u4ee3\u7801\u4e2d\u7684\u4e3b\u8981\u96be\u70b9\uff1a</p> <ul> <li><code>nums</code> \u7684\u5f85\u5408\u5e76\u533a\u95f4\u4e3a <code>[left, right]</code> \uff0c\u800c\u56e0\u4e3a <code>tmp</code> \u53ea\u590d\u5236\u4e86 <code>nums</code> \u8be5\u533a\u95f4\u5143\u7d20\uff0c\u6240\u4ee5 <code>tmp</code> \u5bf9\u5e94\u533a\u95f4\u4e3a <code>[0, right - left]</code> \uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u4ee3\u7801\u4e2d\u5404\u4e2a\u53d8\u91cf\u7684\u542b\u4e49\u3002</li> <li>\u5224\u65ad <code>tmp[i]</code> \u548c <code>tmp[j]</code> \u7684\u5927\u5c0f\u7684\u64cd\u4f5c\u4e2d\uff0c\u8fd8 \u9700\u8003\u8651\u5f53\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\uff0c\u7d22\u5f15\u8d8a\u754c\u7684\u4f18\u5148\u7ea7\u662f\u6700\u9ad8\u7684\uff0c\u4f8b\u5982\u5982\u679c\u5de6\u5b50\u6570\u7ec4\u5df2\u7ecf\u88ab\u5408\u5e76\u5b8c\u4e86\uff0c\u90a3\u4e48\u4e0d\u7528\u7ee7\u7eed\u5224\u65ad\uff0c\u76f4\u63a5\u5408\u5e76\u53f3\u5b50\u6570\u7ec4\u5143\u7d20\u5373\u53ef\u3002</li> </ul>"},{"location":"chapter_sorting/merge_sort/#1152","title":"11.5.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<ul> <li>\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \uff1a\u5212\u5206\u5f62\u6210\u9ad8\u5ea6\u4e3a \\(\\log n\\) \u7684\u9012\u5f52\u6811\uff0c\u6bcf\u5c42\u5408\u5e76\u7684\u603b\u64cd\u4f5c\u6570\u91cf\u4e3a \\(n\\) \uff0c\u603b\u4f53\u4f7f\u7528 \\(O(n \\log n)\\) \u65f6\u95f4\u3002</li> <li>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u9700\u501f\u52a9\u8f85\u52a9\u6570\u7ec4\u5b9e\u73b0\u5408\u5e76\uff0c\u4f7f\u7528 \\(O(n)\\) \u5927\u5c0f\u7684\u989d\u5916\u7a7a\u95f4\uff1b\u9012\u5f52\u6df1\u5ea6\u4e3a \\(\\log n\\) \uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002</li> <li>\u975e\u539f\u5730\u6392\u5e8f\uff1a\u8f85\u52a9\u6570\u7ec4\u9700\u8981\u4f7f\u7528 \\(O(n)\\) \u989d\u5916\u7a7a\u95f4\u3002</li> <li>\u7a33\u5b9a\u6392\u5e8f\uff1a\u5728\u5408\u5e76\u65f6\u53ef\u4fdd\u8bc1\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u4e0d\u53d8\u3002</li> <li>\u975e\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u5bf9\u4e8e\u4efb\u610f\u8f93\u5165\u6570\u636e\uff0c\u5f52\u5e76\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u76f8\u540c\u3002</li> </ul>"},{"location":"chapter_sorting/merge_sort/#1153","title":"11.5.3. \u00a0 \u94fe\u8868\u6392\u5e8f *","text":"<p>\u5f52\u5e76\u6392\u5e8f\u6709\u4e00\u4e2a\u5f88\u7279\u522b\u7684\u4f18\u52bf\uff0c\u7528\u4e8e\u6392\u5e8f\u94fe\u8868\u65f6\u6709\u5f88\u597d\u7684\u6027\u80fd\u8868\u73b0\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u88ab\u4f18\u5316\u81f3 \\(O(1)\\) \uff0c\u8fd9\u662f\u56e0\u4e3a\uff1a</p> <ul> <li>\u7531\u4e8e\u94fe\u8868\u53ef\u4ec5\u901a\u8fc7\u6539\u53d8\u6307\u9488\u6765\u5b9e\u73b0\u7ed3\u70b9\u589e\u5220\uff0c\u56e0\u6b64\u201c\u5c06\u4e24\u4e2a\u77ed\u6709\u5e8f\u94fe\u8868\u5408\u5e76\u4e3a\u4e00\u4e2a\u957f\u6709\u5e8f\u94fe\u8868\u201d\u65e0\u9700\u4f7f\u7528\u989d\u5916\u7a7a\u95f4\uff0c\u5373\u56de\u6eaf\u5408\u5e76\u9636\u6bb5\u4e0d\u7528\u50cf\u6392\u5e8f\u6570\u7ec4\u4e00\u6837\u5efa\u7acb\u8f85\u52a9\u6570\u7ec4 <code>tmp</code> \uff1b</li> <li>\u901a\u8fc7\u4f7f\u7528\u300c\u8fed\u4ee3\u300d\u4ee3\u66ff\u300c\u9012\u5f52\u5212\u5206\u300d\uff0c\u53ef\u7701\u53bb\u9012\u5f52\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\uff1b</li> </ul> <p>\u8be6\u60c5\u53c2\u8003\uff1a148. \u6392\u5e8f\u94fe\u8868</p>"},{"location":"chapter_sorting/quick_sort/","title":"11.4. \u00a0 \u5feb\u901f\u6392\u5e8f","text":"<p>\u300c\u5feb\u901f\u6392\u5e8f Quick Sort\u300d\u662f\u4e00\u79cd\u57fa\u4e8e\u201c\u5206\u6cbb\u601d\u60f3\u201d\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u901f\u5ea6\u5f88\u5feb\u3001\u5e94\u7528\u5f88\u5e7f\u3002</p> <p>\u5feb\u901f\u6392\u5e8f\u7684\u6838\u5fc3\u64cd\u4f5c\u4e3a\u300c\u54e8\u5175\u5212\u5206\u300d\uff0c\u5176\u76ee\u6807\u4e3a\uff1a\u9009\u53d6\u6570\u7ec4\u67d0\u4e2a\u5143\u7d20\u4e3a \u57fa\u51c6\u6570\uff0c\u5c06\u6240\u6709\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u52a8\u81f3\u5176\u5de6\u8fb9\uff0c\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\u79fb\u52a8\u81f3\u5176\u53f3\u8fb9\u3002\u300c\u54e8\u5175\u5212\u5206\u300d\u7684\u5b9e\u73b0\u6d41\u7a0b\u4e3a\uff1a</p> <ol> <li>\u4ee5\u6570\u7ec4\u6700\u5de6\u7aef\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u521d\u59cb\u5316\u4e24\u4e2a\u6307\u9488 <code>i</code> , <code>j</code> \u6307\u5411\u6570\u7ec4\u4e24\u7aef\uff1b</li> <li>\u8bbe\u7f6e\u4e00\u4e2a\u5faa\u73af\uff0c\u6bcf\u8f6e\u4e2d\u4f7f\u7528 <code>i</code> / <code>j</code> \u5206\u522b\u5bfb\u627e\u9996\u4e2a\u6bd4\u57fa\u51c6\u6570\u5927 / \u5c0f\u7684\u5143\u7d20\uff0c\u5e76\u4ea4\u6362\u6b64\u4e24\u5143\u7d20\uff1b</li> <li>\u4e0d\u65ad\u5faa\u73af\u6b65\u9aa4 <code>2.</code> \uff0c\u76f4\u81f3 <code>i</code> , <code>j</code> \u76f8\u9047\u65f6\u8df3\u51fa\uff0c\u6700\u7ec8\u628a\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\uff1b</li> </ol> <p>\u300c\u54e8\u5175\u5212\u5206\u300d\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u539f\u6570\u7ec4\u88ab\u5212\u5206\u6210\u4e24\u4e2a\u90e8\u5206\uff0c\u5373 \u5de6\u5b50\u6570\u7ec4 \u548c \u53f3\u5b50\u6570\u7ec4\uff0c\u4e14\u6ee1\u8db3 \u5de6\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20 < \u57fa\u51c6\u6570 < \u53f3\u5b50\u6570\u7ec4\u4efb\u610f\u5143\u7d20\u3002\u56e0\u6b64\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u53ea\u9700\u8981\u6392\u5e8f\u4e24\u4e2a\u5b50\u6570\u7ec4\u5373\u53ef\u3002</p> <1><2><3><4><5><6><7><8><9>JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> <p></p> quick_sort.java<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.cpp<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(vector<int>& nums, int i, int j) {\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(vector<int>& nums, int left, int right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.py<pre><code>\"\"\" \u54e8\u5175\u5212\u5206 \"\"\"\ndef partition(self, nums, left, right):\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i < j:\nwhile i < j and nums[j] >= nums[left]:\nj -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i < j and nums[i] <= nums[left]:\ni += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n</code></pre> quick_sort.go<pre><code>/* \u54e8\u5175\u5212\u5206 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i < j {\nfor i < j && nums[j] >= nums[left] {\nj-- // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i < j && nums[i] <= nums[left] {\ni++ // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n// \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.js<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums, i, j) {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums, left, right) {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nswap(nums: number[], i: number, j: number): void {\nlet tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\npartition(nums: number[], left: number, right: number): number {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n// \u5143\u7d20\u4ea4\u6362\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.c<pre><code>[class]{quickSort}-[func]{partition}\n</code></pre> quick_sort.cs<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nvoid swap(int[] nums, int i, int j)\n{\nint tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n/* \u54e8\u5175\u5212\u5206 */\nint partition(int[] nums, int left, int right)\n{\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j)\n{\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5143\u7d20\u4ea4\u6362 */\nfunc swap(nums: inout [Int], i: Int, j: Int) {\nlet tmp = nums[i]\nnums[i] = nums[j]\nnums[j] = tmp\n}\n/* \u54e8\u5175\u5212\u5206 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left\nvar j = right\nwhile i < j {\nwhile i < j, nums[j] >= nums[left] {\nj -= 1 // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile i < j, nums[i] <= nums[left] {\ni += 1 // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nswap(nums: &nums, i: i, j: j) // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums: &nums, i: i, j: left) // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.zig<pre><code>// \u5143\u7d20\u4ea4\u6362\nfn swap(nums: []i32, i: usize, j: usize) void {\nvar tmp = nums[i];\nnums[i] = nums[j];\nnums[j] = tmp;\n}\n// \u54e8\u5175\u5212\u5206\nfn partition(nums: []i32, left: usize, right: usize) usize {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i < j) {\nwhile (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> <p>\u5feb\u901f\u6392\u5e8f\u7684\u5206\u6cbb\u601d\u60f3</p> <p>\u54e8\u5175\u5212\u5206\u7684\u5b9e\u8d28\u662f\u5c06 \u4e00\u4e2a\u957f\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898 \u7b80\u5316\u4e3a \u4e24\u4e2a\u77ed\u6570\u7ec4\u7684\u6392\u5e8f\u95ee\u9898\u3002</p>"},{"location":"chapter_sorting/quick_sort/#1141","title":"11.4.1. \u00a0 \u7b97\u6cd5\u6d41\u7a0b","text":"<ol> <li>\u9996\u5148\uff0c\u5bf9\u6570\u7ec4\u6267\u884c\u4e00\u6b21\u300c\u54e8\u5175\u5212\u5206\u300d\uff0c\u5f97\u5230\u5f85\u6392\u5e8f\u7684 \u5de6\u5b50\u6570\u7ec4 \u548c \u53f3\u5b50\u6570\u7ec4\uff1b</li> <li>\u63a5\u4e0b\u6765\uff0c\u5bf9 \u5de6\u5b50\u6570\u7ec4 \u548c \u53f3\u5b50\u6570\u7ec4 \u5206\u522b \u9012\u5f52\u6267\u884c\u300c\u54e8\u5175\u5212\u5206\u300d\u2026\u2026</li> <li>\u76f4\u81f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6 \u7ec8\u6b62\u9012\u5f52\uff0c\u5373\u53ef\u5b8c\u6210\u5bf9\u6574\u4e2a\u6570\u7ec4\u7684\u6392\u5e8f\uff1b</li> </ol> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5feb\u901f\u6392\u5e8f\u548c\u300c\u4e8c\u5206\u67e5\u627e\u300d\u7684\u539f\u7406\u7c7b\u4f3c\uff0c\u90fd\u662f\u4ee5\u5bf9\u6570\u9636\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6765\u7f29\u5c0f\u5904\u7406\u533a\u95f4\u3002</p> <p></p> <p> Fig. \u5feb\u901f\u6392\u5e8f\u6d41\u7a0b </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig quick_sort.java<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.cpp<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(vector<int>& nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.py<pre><code>\"\"\" \u5feb\u901f\u6392\u5e8f \"\"\"\ndef quick_sort(self, nums, left, right):\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left >= right:\nreturn\n# \u54e8\u5175\u5212\u5206\npivot = self.partition(nums, left, right)\n# \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nself.quick_sort(nums, left, pivot - 1)\nself.quick_sort(nums, pivot + 1, right)\n</code></pre> quick_sort.go<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left >= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\npivot := q.partition(nums, left, right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nq.quickSort(nums, left, pivot-1)\nq.quickSort(nums, pivot+1, right)\n}\n</code></pre> quick_sort.js<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums, left, right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nquickSort(nums: number[], left: number, right: number): void {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left >= right) {\nreturn;\n}\n// \u54e8\u5175\u5212\u5206\nconst pivot = this.partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nthis.quickSort(nums, left, pivot - 1);\nthis.quickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.c<pre><code>[class]{quickSort}-[func]{quickSort}\n</code></pre> quick_sort.cs<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nvoid quickSort(int[] nums, int left, int right)\n{\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left >= right)\nreturn;\n// \u54e8\u5175\u5212\u5206\nint pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5feb\u901f\u6392\u5e8f */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif left >= right {\nreturn\n}\n// \u54e8\u5175\u5212\u5206\nlet pivot = partition(nums: &nums, left: left, right: right)\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums: &nums, left: left, right: pivot - 1)\nquickSort(nums: &nums, left: pivot + 1, right: right)\n}\n</code></pre> quick_sort.zig<pre><code>// \u5feb\u901f\u6392\u5e8f\nfn quickSort(nums: []i32, left: usize, right: usize) void {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nif (left >= right) return;\n// \u54e8\u5175\u5212\u5206\nvar pivot = partition(nums, left, right);\n// \u9012\u5f52\u5de6\u5b50\u6570\u7ec4\u3001\u53f3\u5b50\u6570\u7ec4\nquickSort(nums, left, pivot - 1);\nquickSort(nums, pivot + 1, right);\n}\n</code></pre>"},{"location":"chapter_sorting/quick_sort/#1142","title":"11.4.2. \u00a0 \u7b97\u6cd5\u7279\u6027","text":"<p>\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n \\log n)\\) \uff1a\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</p> <p>\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 \\(O(n^2)\\) \uff1a\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u54e8\u5175\u5212\u5206\u64cd\u4f5c\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</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6 \\(O(n)\\) \uff1a\u8f93\u5165\u6570\u7ec4\u5b8c\u5168\u5012\u5e8f\u4e0b\uff0c\u8fbe\u5230\u6700\u5dee\u9012\u5f52\u6df1\u5ea6 \\(n\\) \u3002</p> <p>\u539f\u5730\u6392\u5e8f\uff1a\u53ea\u5728\u9012\u5f52\u4e2d\u4f7f\u7528 \\(O(\\log n)\\) \u5927\u5c0f\u7684\u6808\u5e27\u7a7a\u95f4\u3002</p> <p>\u975e\u7a33\u5b9a\u6392\u5e8f\uff1a\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u53ef\u80fd\u6539\u53d8\u76f8\u7b49\u5143\u7d20\u7684\u76f8\u5bf9\u4f4d\u7f6e\u3002</p> <p>\u81ea\u9002\u5e94\u6392\u5e8f\uff1a\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u3002</p>"},{"location":"chapter_sorting/quick_sort/#1143","title":"11.4.3. \u00a0 \u5feb\u6392\u4e3a\u4ec0\u4e48\u5feb\uff1f","text":"<p>\u4ece\u547d\u540d\u80fd\u591f\u770b\u51fa\uff0c\u5feb\u901f\u6392\u5e8f\u5728\u6548\u7387\u65b9\u9762\u4e00\u5b9a\u201c\u6709\u4e24\u628a\u5237\u5b50\u201d\u3002\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u867d\u7136\u4e0e\u300c\u5f52\u5e76\u6392\u5e8f\u300d\u548c\u300c\u5806\u6392\u5e8f\u300d\u4e00\u81f4\uff0c\u4f46\u5b9e\u9645 \u6548\u7387\u66f4\u9ad8\uff0c\u8fd9\u662f\u56e0\u4e3a\uff1a</p> <ul> <li>\u51fa\u73b0\u6700\u5dee\u60c5\u51b5\u7684\u6982\u7387\u5f88\u4f4e\uff1a\u867d\u7136\u5feb\u901f\u6392\u5e8f\u7684\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(n^2)\\) \uff0c\u4e0d\u5982\u5f52\u5e76\u6392\u5e8f\uff0c\u4f46\u7edd\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u5feb\u901f\u6392\u5e8f\u53ef\u4ee5\u8fbe\u5230 \\(O(n \\log n)\\) \u7684\u590d\u6742\u5ea6\u3002</li> <li>\u7f13\u5b58\u4f7f\u7528\u6548\u7387\u9ad8\uff1a\u54e8\u5175\u5212\u5206\u64cd\u4f5c\u65f6\uff0c\u5c06\u6574\u4e2a\u5b50\u6570\u7ec4\u52a0\u8f7d\u5165\u7f13\u5b58\u4e2d\uff0c\u8bbf\u95ee\u5143\u7d20\u6548\u7387\u5f88\u9ad8\u3002\u800c\u8bf8\u5982\u300c\u5806\u6392\u5e8f\u300d\u9700\u8981\u8df3\u8dc3\u5f0f\u8bbf\u95ee\u5143\u7d20\uff0c\u56e0\u6b64\u4e0d\u5177\u6709\u6b64\u7279\u6027\u3002</li> <li>\u590d\u6742\u5ea6\u7684\u5e38\u6570\u7cfb\u6570\u4f4e\uff1a\u5728\u63d0\u53ca\u7684\u4e09\u79cd\u7b97\u6cd5\u4e2d\uff0c\u5feb\u901f\u6392\u5e8f\u7684 \u6bd4\u8f83\u3001\u8d4b\u503c\u3001\u4ea4\u6362 \u4e09\u79cd\u64cd\u4f5c\u7684\u603b\u4f53\u6570\u91cf\u6700\u5c11\uff08\u7c7b\u4f3c\u4e8e\u300c\u63d2\u5165\u6392\u5e8f\u300d\u5feb\u4e8e\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u7684\u539f\u56e0\uff09\u3002</li> </ul>"},{"location":"chapter_sorting/quick_sort/#1144","title":"11.4.4. \u00a0 \u57fa\u51c6\u6570\u4f18\u5316","text":"<p>\u666e\u901a\u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\u7684\u65f6\u95f4\u6548\u7387\u53d8\u5dee\u3002\u4e3e\u4e2a\u6781\u7aef\u4f8b\u5b50\uff0c\u5047\u8bbe\u8f93\u5165\u6570\u7ec4\u662f\u5b8c\u5168\u5012\u5e8f\u7684\uff0c\u7531\u4e8e\u6211\u4eec\u9009\u53d6\u6700\u5de6\u7aef\u5143\u7d20\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\u4ece\u800c \u5de6\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(n - 1\\)\u3001\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a \\(0\\) \u3002\u8fd9\u6837\u8fdb\u4e00\u6b65\u9012\u5f52\u4e0b\u53bb\uff0c\u6bcf\u8f6e\u54e8\u5175\u5212\u5206\u540e\u7684\u53f3\u5b50\u6570\u7ec4\u957f\u5ea6\u90fd\u4e3a \\(0\\) \uff0c\u5206\u6cbb\u7b56\u7565\u5931\u6548\uff0c\u5feb\u901f\u6392\u5e8f\u9000\u5316\u4e3a\u300c\u5192\u6ce1\u6392\u5e8f\u300d\u4e86\u3002</p> <p>\u4e3a\u4e86\u5c3d\u91cf\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\u4e00\u4e0b\u57fa\u51c6\u6570\u7684\u9009\u53d6\u7b56\u7565\u3002\u9996\u5148\uff0c\u5728\u54e8\u5175\u5212\u5206\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5 \u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u57fa\u51c6\u6570\u3002\u4f46\u5982\u679c\u8fd0\u6c14\u5f88\u5dee\uff0c\u6bcf\u6b21\u90fd\u9009\u62e9\u5230\u6bd4\u8f83\u5dee\u7684\u57fa\u51c6\u6570\uff0c\u90a3\u4e48\u6548\u7387\u4f9d\u7136\u4e0d\u597d\u3002</p> <p>\u8fdb\u4e00\u6b65\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u7ec4\u4e2d\u9009\u53d6 3 \u4e2a\u5019\u9009\u5143\u7d20\uff08\u4e00\u822c\u4e3a\u6570\u7ec4\u7684\u9996\u3001\u5c3e\u3001\u4e2d\u70b9\u5143\u7d20\uff09\uff0c\u5e76\u5c06\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\u4f5c\u4e3a\u57fa\u51c6\u6570\uff0c\u8fd9\u6837\u57fa\u51c6\u6570\u201c\u65e2\u4e0d\u5927\u4e5f\u4e0d\u5c0f\u201d\u7684\u6982\u7387\u5c31\u5927\u5927\u63d0\u5347\u4e86\u3002\u5f53\u7136\uff0c\u5982\u679c\u6570\u7ec4\u5f88\u957f\u7684\u8bdd\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u9009\u53d6\u66f4\u591a\u5019\u9009\u5143\u7d20\uff0c\u6765\u8fdb\u4e00\u6b65\u63d0\u5347\u7b97\u6cd5\u7684\u7a33\u5065\u6027\u3002\u91c7\u53d6\u8be5\u65b9\u6cd5\u540e\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n^2)\\) \u7684\u6982\u7387\u6781\u4f4e\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig quick_sort.java<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.cpp<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(vector<int>& nums, int left, int mid, int right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(vector<int>& nums, int left, int right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.py<pre><code>\"\"\" \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 \"\"\"\ndef median_three(self, nums, left, mid, right):\n# \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n# \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif (nums[left] < nums[mid]) ^ (nums[left] < nums[right]):\nreturn left\nelif (nums[mid] < nums[left]) ^ (nums[mid] < nums[right]):\nreturn mid\nreturn right\n\"\"\" \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 \"\"\"\ndef partition(self, nums, left, right):\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed = self.median_three(nums, left, (left + right) // 2, right)\n# \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n# \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j = left, right\nwhile i < j:\nwhile i < j and nums[j] >= nums[left]:\nj -= 1 # \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile i < j and nums[i] <= nums[left]:\ni += 1 # \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n# \u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n# \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i # \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n</code></pre> quick_sort.go<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\uff08!= \u5728\u8fd9\u91cc\u8d77\u5230\u5f02\u6216\u7684\u4f5c\u7528\uff09\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\nreturn left\n} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\nreturn mid\n}\nreturn right\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09*/\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nmed := q.medianThree(nums, left, (left+right)/2, right)\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nnums[left], nums[med] = nums[med], nums[left]\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\ni, j := left, right\nfor i < j {\nfor i < j && nums[j] >= nums[left] {\nj-- //\u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nfor i < j && nums[i] <= nums[left] {\ni++ //\u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\n//\u5143\u7d20\u4ea4\u6362\nnums[i], nums[j] = nums[j], nums[i]\n}\n//\u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nnums[i], nums[left] = nums[left], nums[i]\nreturn i //\u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.js<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(nums, left, mid, right) {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right])) return left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right])) return mid;\nelse return right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums, left, right) {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = this.medianThree(nums, left, Math.floor((left + right) / 2), right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) j--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left]) i++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.ts<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nmedianThree(nums: number[], left: number, mid: number, right: number): number {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif (Number(nums[left] < nums[mid]) ^ Number(nums[left] < nums[right])) {\nreturn left;\n} else if (Number(nums[mid] < nums[left]) ^ Number(nums[mid] < nums[right])) {\nreturn mid;\n} else {\nreturn right;\n}\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\npartition(nums: number[], left: number, right: number): number {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = this.medianThree(nums, left, Math.floor((left + right) / 2), right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nthis.swap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nlet i = left, j = right;\nwhile (i < j) {\nwhile (i < j && nums[j] >= nums[left]) {\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nwhile (i < j && nums[i] <= nums[left]) {\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\n}\nthis.swap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nthis.swap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.c<pre><code>[class]{quickSortMedian}-[func]{medianThree}\n[class]{quickSortMedian}-[func]{partition}\n</code></pre> quick_sort.cs<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nint medianThree(int[] nums, int left, int mid, int right)\n{\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] < nums[mid]) ^ (nums[left] < nums[right]))\nreturn left;\nelse if ((nums[mid] < nums[left]) ^ (nums[mid] < nums[right]))\nreturn mid;\nelse\nreturn right;\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nint partition(int[] nums, int left, int right)\n{\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nint med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nint i = left, j = right;\nwhile (i < j)\n{\nwhile (i < j && nums[j] >= nums[left])\nj--; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j && nums[i] <= nums[left])\ni++; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre> quick_sort.swift<pre><code>/* \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\nif (nums[left] < nums[mid]) != (nums[left] < nums[right]) {\nreturn left\n} else if (nums[mid] < nums[left]) != (nums[mid] < nums[right]) {\nreturn mid\n} else {\nreturn right\n}\n}\n/* \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09 */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nlet med = medianThree(nums: nums, left: left, mid: (left + right) / 2, right: right)\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums: &nums, i: left, j: med)\nreturn partition(nums: &nums, left: left, right: right)\n}\n</code></pre> quick_sort.zig<pre><code>// \u9009\u53d6\u4e09\u4e2a\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nfn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize {\n// \u6b64\u5904\u4f7f\u7528\u5f02\u6216\u8fd0\u7b97\u6765\u7b80\u5316\u4ee3\u7801\n// \u5f02\u6216\u89c4\u5219\u4e3a 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1\nif ((nums[left] < nums[mid]) != (nums[left] < nums[right])) {\nreturn left;\n} else if ((nums[mid] < nums[left]) != (nums[mid] < nums[right])) {\nreturn mid;\n} else {\nreturn right;\n}\n}\n// \u54e8\u5175\u5212\u5206\uff08\u4e09\u6570\u53d6\u4e2d\u503c\uff09\nfn partition(nums: []i32, left: usize, right: usize) usize {\n// \u9009\u53d6\u4e09\u4e2a\u5019\u9009\u5143\u7d20\u7684\u4e2d\u4f4d\u6570\nvar med = medianThree(nums, left, (left + right) / 2, right);\n// \u5c06\u4e2d\u4f4d\u6570\u4ea4\u6362\u81f3\u6570\u7ec4\u6700\u5de6\u7aef\nswap(nums, left, med);\n// \u4ee5 nums[left] \u4f5c\u4e3a\u57fa\u51c6\u6570\nvar i = left;\nvar j = right;\nwhile (i < j) {\nwhile (i < j and nums[j] >= nums[left]) j -= 1; // \u4ece\u53f3\u5411\u5de6\u627e\u9996\u4e2a\u5c0f\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nwhile (i < j and nums[i] <= nums[left]) i += 1; // \u4ece\u5de6\u5411\u53f3\u627e\u9996\u4e2a\u5927\u4e8e\u57fa\u51c6\u6570\u7684\u5143\u7d20\nswap(nums, i, j); // \u4ea4\u6362\u8fd9\u4e24\u4e2a\u5143\u7d20\n}\nswap(nums, i, left); // \u5c06\u57fa\u51c6\u6570\u4ea4\u6362\u81f3\u4e24\u5b50\u6570\u7ec4\u7684\u5206\u754c\u7ebf\nreturn i; // \u8fd4\u56de\u57fa\u51c6\u6570\u7684\u7d22\u5f15\n}\n</code></pre>"},{"location":"chapter_sorting/quick_sort/#1145","title":"11.4.5. \u00a0 \u5c3e\u9012\u5f52\u4f18\u5316","text":"<p>\u666e\u901a\u5feb\u901f\u6392\u5e8f\u5728\u67d0\u4e9b\u8f93\u5165\u4e0b\u7684\u7a7a\u95f4\u6548\u7387\u53d8\u5dee\u3002\u4ecd\u7136\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\u90a3\u4e48\u5c06\u5f62\u6210\u4e00\u4e2a\u9ad8\u5ea6\u4e3a \\(n - 1\\) \u7684\u9012\u5f52\u6811\uff0c\u6b64\u65f6\u4f7f\u7528\u7684\u6808\u5e27\u7a7a\u95f4\u5927\u5c0f\u52a3\u5316\u81f3 \\(O(n)\\) \u3002</p> <p>\u4e3a\u4e86\u907f\u514d\u6808\u5e27\u7a7a\u95f4\u7684\u7d2f\u79ef\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6bcf\u8f6e\u54e8\u5175\u6392\u5e8f\u5b8c\u6210\u540e\uff0c\u5224\u65ad\u4e24\u4e2a\u5b50\u6570\u7ec4\u7684\u957f\u5ea6\u5927\u5c0f\uff0c\u4ec5\u9012\u5f52\u6392\u5e8f\u8f83\u77ed\u7684\u5b50\u6570\u7ec4\u3002\u7531\u4e8e\u8f83\u77ed\u7684\u5b50\u6570\u7ec4\u957f\u5ea6\u4e0d\u4f1a\u8d85\u8fc7 \\(\\frac{n}{2}\\) \uff0c\u56e0\u6b64\u8fd9\u6837\u505a\u80fd\u4fdd\u8bc1\u9012\u5f52\u6df1\u5ea6\u4e0d\u8d85\u8fc7 \\(\\log n\\) \uff0c\u5373\u6700\u5dee\u7a7a\u95f4\u590d\u6742\u5ea6\u88ab\u4f18\u5316\u81f3 \\(O(\\log n)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig quick_sort.java<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.cpp<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(vector<int>& nums, int left, int right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.py<pre><code>\"\"\" \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 \"\"\"\ndef quick_sort(self, nums, left, right):\n# \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right:\n# \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot = self.partition(nums, left, right)\n# \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot - left < right - pivot:\nself.quick_sort(nums, left, pivot - 1) # \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 # \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\nelse:\nself.quick_sort(nums, pivot + 1, right) # \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1 # \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n</code></pre> quick_sort.go<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09*/\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nfor left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\npivot := q.partition(nums, left, right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif pivot-left < right-pivot {\nq.quickSort(nums, left, pivot-1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nq.quickSort(nums, pivot+1, right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1 // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.js<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums, left, right) {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.ts<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nquickSort(nums: number[], left: number, right: number): void {\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = this.partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nthis.quickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nthis.quickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.c<pre><code>[class]{quickSortTailCall}-[func]{quickSort}\n</code></pre> quick_sort.cs<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nvoid quickSort(int[] nums, int left, int right)\n{\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile (left < right)\n{\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nint pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot)\n{\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n}\nelse\n{\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.swift<pre><code>/* \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09 */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\nvar left = left\nvar right = right\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\nwhile left < right {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nlet pivot = partition(nums: &nums, left: left, right: right)\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left) < (right - pivot) {\nquickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1 // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1 // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre> quick_sort.zig<pre><code>// \u5feb\u901f\u6392\u5e8f\uff08\u5c3e\u9012\u5f52\u4f18\u5316\uff09\nfn quickSort(nums: []i32, left_: usize, right_: usize) void {\nvar left = left_;\nvar right = right_;\n// \u5b50\u6570\u7ec4\u957f\u5ea6\u4e3a 1 \u65f6\u7ec8\u6b62\u9012\u5f52\nwhile (left < right) {\n// \u54e8\u5175\u5212\u5206\u64cd\u4f5c\nvar pivot = partition(nums, left, right);\n// \u5bf9\u4e24\u4e2a\u5b50\u6570\u7ec4\u4e2d\u8f83\u77ed\u7684\u90a3\u4e2a\u6267\u884c\u5feb\u6392\nif (pivot - left < right - pivot) {\nquickSort(nums, left, pivot - 1); // \u9012\u5f52\u6392\u5e8f\u5de6\u5b50\u6570\u7ec4\nleft = pivot + 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [pivot + 1, right]\n} else {\nquickSort(nums, pivot + 1, right); // \u9012\u5f52\u6392\u5e8f\u53f3\u5b50\u6570\u7ec4\nright = pivot - 1; // \u5269\u4f59\u5f85\u6392\u5e8f\u533a\u95f4\u4e3a [left, pivot - 1]\n}\n}\n}\n</code></pre>"},{"location":"chapter_sorting/summary/","title":"11.6. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u5192\u6ce1\u6392\u5e8f\u901a\u8fc7\u4ea4\u6362\u76f8\u90bb\u5143\u7d20\u6765\u5b9e\u73b0\u6392\u5e8f\u3002\u901a\u8fc7\u589e\u52a0\u6807\u5fd7\u4f4d\u5b9e\u73b0\u63d0\u524d\u8fd4\u56de\uff0c\u6211\u4eec\u53ef\u5c06\u5192\u6ce1\u6392\u5e8f\u7684\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(N)\\) \u3002</li> <li>\u63d2\u5165\u6392\u5e8f\u6bcf\u8f6e\u5c06\u5f85\u6392\u5e8f\u533a\u95f4\u5185\u5143\u7d20\u63d2\u5165\u81f3\u5df2\u6392\u5e8f\u533a\u95f4\u7684\u6b63\u786e\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u6392\u5e8f\u3002\u63d2\u5165\u6392\u5e8f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u867d\u4e3a \\(O(N^2)\\) \uff0c\u4f46\u56e0\u4e3a\u603b\u4f53\u64cd\u4f5c\u5c11\u800c\u5f88\u53d7\u6b22\u8fce\uff0c\u4e00\u822c\u7528\u4e8e\u5c0f\u6570\u636e\u91cf\u7684\u6392\u5e8f\u5de5\u4f5c\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\u4ece\u800c\u5bfc\u81f4\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(N^2)\\) \uff0c\u901a\u8fc7\u5f15\u5165\u4e2d\u4f4d\u6570\u57fa\u51c6\u6570\u6216\u968f\u673a\u57fa\u51c6\u6570\u53ef\u5927\u5927\u964d\u4f4e\u52a3\u5316\u6982\u7387\u3002\u5c3e\u9012\u5f52\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u51cf\u5c0f\u9012\u5f52\u6df1\u5ea6\uff0c\u5c06\u7a7a\u95f4\u590d\u6742\u5ea6\u4f18\u5316\u81f3 \\(O(\\log N)\\) \u3002</li> <li>\u5f52\u5e76\u6392\u5e8f\u5305\u542b\u5212\u5206\u548c\u5408\u5e76\u4e24\u4e2a\u9636\u6bb5\uff0c\u662f\u5206\u800c\u6cbb\u4e4b\u7684\u6807\u51c6\u4f53\u73b0\u3002\u5bf9\u4e8e\u5f52\u5e76\u6392\u5e8f\uff0c\u6392\u5e8f\u6570\u7ec4\u9700\u8981\u501f\u52a9\u8f85\u52a9\u6570\u7ec4\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a \\(O(N)\\) \uff1b\u800c\u6392\u5e8f\u94fe\u8868\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u88ab\u4f18\u5316\u81f3 \\(O(1)\\) \u3002</li> <li>\u4e0b\u56fe\u603b\u7ed3\u5bf9\u6bd4\u4e86\u5404\u4e2a\u6392\u5e8f\u7b97\u6cd5\u7684\u8fd0\u884c\u6548\u7387\u4e0e\u7279\u6027\u3002\u5176\u4e2d\uff0c\u6876\u6392\u5e8f\u4e2d \\(k\\) \u4e3a\u6876\u7684\u6570\u91cf\uff1b\u57fa\u6570\u6392\u5e8f\u4ec5\u9002\u7528\u4e8e\u6b63\u6574\u6570\u3001\u5b57\u7b26\u4e32\u3001\u7279\u5b9a\u683c\u5f0f\u7684\u6d6e\u70b9\u6570\uff0c\\(k\\) \u4e3a\u6700\u5927\u6570\u5b57\u7684\u4f4d\u6570\u3002</li> </ul> <p> Fig. \u6392\u5e8f\u7b97\u6cd5\u5bf9\u6bd4 </p> <ul> <li>\u603b\u4f53\u6765\u770b\uff0c\u6211\u4eec\u8ffd\u6c42\u8fd0\u884c\u5feb\u3001\u7a33\u5b9a\u3001\u539f\u5730\u3001\u6b63\u5411\u81ea\u9002\u5e94\u6027\u7684\u6392\u5e8f\u3002\u663e\u7136\uff0c\u5982\u540c\u5176\u5b83\u6570\u636e\u7ed3\u6784\u4e0e\u7b97\u6cd5\u4e00\u6837\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e9b\u6761\u4ef6\u7684\u6392\u5e8f\u7b97\u6cd5\u5e76\u4e0d\u5b58\u5728\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u95ee\u9898\u7279\u70b9\u6765\u9009\u62e9\u6392\u5e8f\u7b97\u6cd5\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/deque/","title":"5.3. \u00a0 \u53cc\u5411\u961f\u5217\uff08Deque\uff09","text":"<p>\u5bf9\u4e8e\u961f\u5217\uff0c\u6211\u4eec\u53ea\u80fd\u5728\u5934\u90e8\u5220\u9664\u6216\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\uff0c\u800c\u300c\u53cc\u5411\u961f\u5217 Deque\u300d\u66f4\u52a0\u7075\u6d3b\uff0c\u5728\u5176\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u80fd\u6267\u884c\u5143\u7d20\u6dfb\u52a0\u6216\u5220\u9664\u64cd\u4f5c\u3002</p> <p></p> <p> Fig. \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\u89c1\u4e0b\u8868\uff0c\u65b9\u6cd5\u540d\u9700\u6839\u636e\u7279\u5b9a\u8bed\u8a00\u6765\u786e\u5b9a\u3002</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)\\) pollFirst() \u5220\u9664\u961f\u9996\u5143\u7d20 \\(O(1)\\) pollLast() \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)\\) size() \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 \\(O(1)\\) isEmpty() \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a \\(O(1)\\) <p>\u76f8\u540c\u5730\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5b9e\u73b0\u597d\u7684\u53cc\u5411\u961f\u5217\u7c7b\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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 pollFirst = deque.pollFirst(); // \u961f\u9996\u5143\u7d20\u51fa\u961f\nint pollLast = deque.pollLast(); // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size = deque.size();\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = deque.isEmpty();\n</code></pre> deque.cpp<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\ndeque<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.py<pre><code>\"\"\" \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 \"\"\"\nduque = deque()\n\"\"\" \u5143\u7d20\u5165\u961f \"\"\"\nduque.append(2) # \u6dfb\u52a0\u81f3\u961f\u5c3e\nduque.append(5)\nduque.append(4)\nduque.appendleft(3) # \u6dfb\u52a0\u81f3\u961f\u9996\nduque.appendleft(1)\n\"\"\" \u8bbf\u95ee\u5143\u7d20 \"\"\"\nfront = duque[0] # \u961f\u9996\u5143\u7d20\nrear = duque[-1] # \u961f\u5c3e\u5143\u7d20\n\"\"\" \u5143\u7d20\u51fa\u961f \"\"\"\npop_front = duque.popleft() # \u961f\u9996\u5143\u7d20\u51fa\u961f\npop_rear = duque.pop() # \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n\"\"\" \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 \"\"\"\nsize = len(duque)\n\"\"\" \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a \"\"\"\nis_empty = len(duque) == 0\n</code></pre> deque_test.go<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u53cc\u5411\u961f\u5217\u4f7f\u7528\ndeque := list.New()\n/* \u5143\u7d20\u5165\u961f */\ndeque.PushBack(2) // \u6dfb\u52a0\u81f3\u961f\u5c3e\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3) // \u6dfb\u52a0\u81f3\u961f\u9996\ndeque.PushFront(1)\n/* \u8bbf\u95ee\u5143\u7d20 */\nfront := deque.Front() // \u961f\u9996\u5143\u7d20\nrear := deque.Back() // \u961f\u5c3e\u5143\u7d20\n/* \u5143\u7d20\u51fa\u961f */\ndeque.Remove(front) // \u961f\u9996\u5143\u7d20\u51fa\u961f\ndeque.Remove(rear) // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize := deque.Len()\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := deque.Len() == 0\n</code></pre> deque.js<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque = [];\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront = deque.shift();\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack = deque.pop();\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n</code></pre> deque.ts<pre><code>/* \u521d\u59cb\u5316\u53cc\u5411\u961f\u5217 */\n// TypeScript \u6ca1\u6709\u5185\u7f6e\u7684\u53cc\u7aef\u961f\u5217\uff0c\u53ea\u80fd\u628a Array \u5f53\u4f5c\u53cc\u7aef\u961f\u5217\u6765\u4f7f\u7528\nconst deque: number[] = [];\n/* \u5143\u7d20\u5165\u961f */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cunshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\ndeque.unshift(3);\ndeque.unshift(1);\nconsole.log(\"\u53cc\u5411\u961f\u5217 deque = \", deque);\n/* \u8bbf\u95ee\u5143\u7d20 */\nconst peekFirst: number = deque[0];\nconsole.log(\"\u961f\u9996\u5143\u7d20 peekFirst = \" + peekFirst);\nconst peekLast: number = deque[deque.length - 1];\nconsole.log(\"\u961f\u5c3e\u5143\u7d20 peekLast = \" + peekLast);\n/* \u5143\u7d20\u51fa\u961f */\n// \u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u662f\u6570\u7ec4\uff0cshift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst popFront: number = deque.shift() as number;\nconsole.log(\"\u961f\u9996\u51fa\u961f\u5143\u7d20 popFront = \" + popFront + \"\uff0c\u961f\u9996\u51fa\u961f\u540e deque = \" + deque);\nconst popBack: number = deque.pop() as number;\nconsole.log(\"\u961f\u5c3e\u51fa\u961f\u5143\u7d20 popBack = \" + popBack + \"\uff0c\u961f\u5c3e\u51fa\u961f\u540e deque = \" + deque);\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nconst size: number = deque.length;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u957f\u5ea6 size = \" + size);\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst isEmpty: boolean = size === 0;\nconsole.log(\"\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a = \" + isEmpty);\n</code></pre> deque.c<pre><code>\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.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 pollFirst \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet pollFirst = deque.removeFirst() // \u961f\u9996\u5143\u7d20\u51fa\u961f\nlet pollLast = deque.removeLast() // \u961f\u5c3e\u5143\u7d20\u51fa\u961f\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = deque.count\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = deque.isEmpty\n</code></pre> deque.zig<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2. \u00a0 \u53cc\u5411\u961f\u5217\u5b9e\u73b0 *","text":"<p>\u4e0e\u961f\u5217\u7c7b\u4f3c\uff0c\u53cc\u5411\u961f\u5217\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u6216\u6570\u7ec4\u6765\u5b9e\u73b0\u3002</p>"},{"location":"chapter_stack_and_queue/deque/#_1","title":"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u5b9e\u73b0","text":"<p>\u56de\u5fc6\u4e0a\u8282\u5185\u5bb9\uff0c\u7531\u4e8e\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664\u94fe\u8868\u5934\u7ed3\u70b9\uff08\u5bf9\u5e94\u51fa\u961f\u64cd\u4f5c\uff09\uff0c\u4ee5\u53ca\u5728\u94fe\u8868\u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0\u65b0\u7ed3\u70b9\uff08\u5bf9\u5e94\u5165\u961f\u64cd\u4f5c\uff09\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528\u666e\u901a\u5355\u5411\u94fe\u8868\u6765\u5b9e\u73b0\u961f\u5217\u3002</p> <p>\u800c\u53cc\u5411\u961f\u5217\u7684\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u53ef\u4ee5\u6267\u884c\u5165\u961f\u4e0e\u51fa\u961f\u64cd\u4f5c\uff0c\u6362\u8a00\u4e4b\uff0c\u53cc\u5411\u961f\u5217\u7684\u64cd\u4f5c\u662f\u201c\u9996\u5c3e\u5bf9\u79f0\u201d\u7684\uff0c\u4e5f\u9700\u8981\u5b9e\u73b0\u53e6\u4e00\u4e2a\u5bf9\u79f0\u65b9\u5411\u7684\u64cd\u4f5c\u3002\u56e0\u6b64\uff0c\u53cc\u5411\u961f\u5217\u9700\u8981\u4f7f\u7528\u300c\u53cc\u5411\u94fe\u8868\u300d\u6765\u5b9e\u73b0\u3002</p> <p>\u6211\u4eec\u5c06\u53cc\u5411\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u548c\u5c3e\u7ed3\u70b9\u5206\u522b\u770b\u4f5c\u53cc\u5411\u961f\u5217\u7684\u961f\u9996\u548c\u961f\u5c3e\uff0c\u5e76\u4e14\u5b9e\u73b0\u5728\u4e24\u7aef\u90fd\u80fd\u6dfb\u52a0\u4e0e\u5220\u9664\u7ed3\u70b9\u3002</p> LinkedListDequepushLast()pushFirst()pollLast()pollFirst() <p></p> <p></p> <p></p> <p></p> <p></p> <p>\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linkedlist_deque.java<pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9 */\nclass ListNode {\nint val; // \u7ed3\u70b9\u503c\nListNode next; // \u540e\u7ee7\u7ed3\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode prev; // \u524d\u9a71\u7ed3\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nListNode(int val) {\nthis.val = val;\nprev = next = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate ListNode front, rear; // \u5934\u7ed3\u70b9 front \uff0c\u5c3e\u7ed3\u70b9 rear\nprivate int size = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\npublic LinkedListDeque() {\nfront = rear = null;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn size;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate void push(int num, boolean isFront) {\nListNode node = new ListNode(num);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (isEmpty())\nfront = rear = node;\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if (isFront) {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront.prev = node;\nnode.next = front;\nfront = node; // \u66f4\u65b0\u5934\u7ed3\u70b9\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear.next = node;\nnode.prev = rear;\nrear = node; // \u66f4\u65b0\u5c3e\u7ed3\u70b9\n}\nsize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\npush(num, true);\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\npush(num, false);\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate Integer poll(boolean isFront) {\n// \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\nif (isEmpty())\nreturn null;\nint val;\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif (isFront) {\nval = front.val; // \u6682\u5b58\u5934\u7ed3\u70b9\u503c\n// \u5220\u9664\u5934\u7ed3\u70b9\nListNode fNext = front.next;\nif (fNext != null) {\nfNext.prev = null;\nfront.next = null;\n}\nfront = fNext; // \u66f4\u65b0\u5934\u7ed3\u70b9\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n} else {\nval = rear.val; // \u6682\u5b58\u5c3e\u7ed3\u70b9\u503c\n// \u5220\u9664\u5c3e\u7ed3\u70b9\nListNode rPrev = rear.prev;\nif (rPrev != null) {\nrPrev.next = null;\nrear.prev = null;\n}\nrear = rPrev; // \u66f4\u65b0\u5c3e\u7ed3\u70b9\n}\nsize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic Integer pollFirst() {\nreturn poll(true);\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic Integer pollLast() {\nreturn poll(false);\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic Integer peekFirst() {\nreturn isEmpty() ? null : front.val;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic Integer peekLast() {\nreturn isEmpty() ? null : rear.val;\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_deque.cpp<pre><code>\n</code></pre> linkedlist_deque.py<pre><code>\n</code></pre> linkedlist_deque.go<pre><code>\n</code></pre> linkedlist_deque.js<pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9 */\nclass ListNode {\nprev; // \u524d\u9a71\u7ed3\u70b9\u5f15\u7528 (\u6307\u9488)\nnext; // \u540e\u7ee7\u7ed3\u70b9\u5f15\u7528 (\u6307\u9488)\nval; // \u7ed3\u70b9\u503c\nconstructor(val) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nfront; // \u5934\u7ed3\u70b9 front\nrear; // \u5c3e\u7ed3\u70b9 rear\nlen; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nconstructor() {\nthis.front = null;\nthis.rear = null;\nthis.len = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.len === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.rear.next = node;\nnode.prev = this.rear;\nthis.rear = node; // \u66f4\u65b0\u5c3e\u7ed3\u70b9\n}\nthis.len++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val) {\nconst node = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.len === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.front.prev = node;\nnode.next = this.front;\nthis.front = node; // \u66f4\u65b0\u5934\u7ed3\u70b9\n}\nthis.len++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npollLast() {\nif (this.len === 0) {\nreturn null;\n}\nconst value = this.rear.val; // \u5b58\u50a8\u5c3e\u7ed3\u70b9\u503c\n// \u5220\u9664\u5c3e\u7ed3\u70b9\nlet temp = this.rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.rear.prev = null;\n}\nthis.rear = temp; // \u66f4\u65b0\u5c3e\u7ed3\u70b9\nthis.len--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npollFirst() {\nif (this.len === 0) {\nreturn null;\n}\nconst value = this.front.val; // \u5b58\u50a8\u5c3e\u7ed3\u70b9\u503c\n// \u5220\u9664\u5934\u7ed3\u70b9\nlet temp = this.front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.front.next = null;\n}\nthis.front = temp; // \u66f4\u65b0\u5934\u7ed3\u70b9\nthis.len--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast() {\nreturn this.len === 0 ? null : this.rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst() {\nreturn this.len === 0 ? null : this.front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize() {\nreturn this.len;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.len === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint() {\nconst arr = [];\nlet temp = this.front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log(\"[\" + arr.join(\", \") + \"]\");\n}\n}\n</code></pre> linkedlist_deque.ts<pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9 */\nclass ListNode {\nprev: ListNode; // \u524d\u9a71\u7ed3\u70b9\u5f15\u7528 (\u6307\u9488)\nnext: ListNode; // \u540e\u7ee7\u7ed3\u70b9\u5f15\u7528 (\u6307\u9488)\nval: number; // \u7ed3\u70b9\u503c\nconstructor(val: number) {\nthis.val = val;\nthis.next = null;\nthis.prev = null;\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nfront: ListNode; // \u5934\u7ed3\u70b9 front\nrear: ListNode; // \u5c3e\u7ed3\u70b9 rear\nlen: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\nconstructor() {\nthis.front = null;\nthis.rear = null;\nthis.len = 0;\n}\n/* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\npushLast(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.len === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nthis.rear.next = node;\nnode.prev = this.rear;\nthis.rear = node; // \u66f4\u65b0\u5c3e\u7ed3\u70b9\n}\nthis.len++;\n}\n/* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\npushFirst(val: number): void {\nconst node: ListNode = new ListNode(val);\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif (this.len === 0) {\nthis.front = node;\nthis.rear = node;\n} else {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nthis.front.prev = node;\nnode.next = this.front;\nthis.front = node; // \u66f4\u65b0\u5934\u7ed3\u70b9\n}\nthis.len++;\n}\n/* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\npollLast(): number {\nif (this.len === 0) {\nreturn null;\n}\nconst value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u7ed3\u70b9\u503c\n// \u5220\u9664\u5c3e\u7ed3\u70b9\nlet temp: ListNode = this.rear.prev;\nif (temp !== null) {\ntemp.next = null;\nthis.rear.prev = null;\n}\nthis.rear = temp; // \u66f4\u65b0\u5c3e\u7ed3\u70b9\nthis.len--;\nreturn value;\n}\n/* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\npollFirst(): number {\nif (this.len === 0) {\nreturn null;\n}\nconst value: number = this.front.val; // \u5b58\u50a8\u5c3e\u7ed3\u70b9\u503c\n// \u5220\u9664\u5934\u7ed3\u70b9\nlet temp: ListNode = this.front.next;\nif (temp !== null) {\ntemp.prev = null;\nthis.front.next = null;\n}\nthis.front = temp; // \u66f4\u65b0\u5934\u7ed3\u70b9\nthis.len--;\nreturn value;\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npeekLast(): number {\nreturn this.len === 0 ? null : this.rear.val;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeekFirst(): number {\nreturn this.len === 0 ? null : this.front.val;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nsize(): number {\nreturn this.len;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.len === 0;\n}\n/* \u6253\u5370\u53cc\u5411\u961f\u5217 */\nprint(): void {\nconst arr: number[] = [];\nlet temp: ListNode = this.front;\nwhile (temp !== null) {\narr.push(temp.val);\ntemp = temp.next;\n}\nconsole.log(\"[\" + arr.join(\", \") + \"]\");\n}\n}\n</code></pre> linkedlist_deque.c<pre><code>\n</code></pre> linkedlist_deque.cs<pre><code>[class]{ListNode}-[func]{}\n[class]{LinkedListDeque}-[func]{}\n</code></pre> linkedlist_deque.swift<pre><code>/* \u53cc\u5411\u94fe\u8868\u7ed3\u70b9 */\nclass ListNode {\nvar val: Int // \u7ed3\u70b9\u503c\nvar next: ListNode? // \u540e\u7ee7\u7ed3\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\nvar prev: ListNode? // \u524d\u9a71\u7ed3\u70b9\u5f15\u7528\uff08\u6307\u9488\uff09\ninit(val: Int) {\nself.val = val\n}\n}\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\nprivate var front: ListNode? // \u5934\u7ed3\u70b9 front\nprivate var rear: ListNode? // \u5c3e\u7ed3\u70b9 rear\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\ninit() {\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u961f\u64cd\u4f5c */\nprivate func push(num: Int, isFront: Bool) {\nlet node = ListNode(val: num)\n// \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front, rear \u90fd\u6307\u5411 node\nif isEmpty() {\nfront = node\nrear = node\n}\n// \u961f\u9996\u5165\u961f\u64cd\u4f5c\nelse if isFront {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\nfront?.prev = node\nnode.next = front\nfront = node // \u66f4\u65b0\u5934\u7ed3\u70b9\n}\n// \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\nelse {\n// \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\nrear?.next = node\nnode.prev = rear\nrear = node // \u66f4\u65b0\u5c3e\u7ed3\u70b9\n}\nqueSize += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\npush(num: num, isFront: true)\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\npush(num: num, isFront: false)\n}\n/* \u51fa\u961f\u64cd\u4f5c */\nprivate func poll(isFront: Bool) -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nlet val: Int\n// \u961f\u9996\u51fa\u961f\u64cd\u4f5c\nif isFront {\nval = front!.val // \u6682\u5b58\u5934\u7ed3\u70b9\u503c\n// \u5220\u9664\u5934\u7ed3\u70b9\nlet fNext = front?.next\nif fNext != nil {\nfNext?.prev = nil\nfront?.next = nil\n}\nfront = fNext // \u66f4\u65b0\u5934\u7ed3\u70b9\n}\n// \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\nelse {\nval = rear!.val // \u6682\u5b58\u5c3e\u7ed3\u70b9\u503c\n// \u5220\u9664\u5c3e\u7ed3\u70b9\nlet rPrev = rear?.prev\nif rPrev != nil {\nrPrev?.next = nil\nrear?.prev = nil\n}\nrear = rPrev // \u66f4\u65b0\u5c3e\u7ed3\u70b9\n}\nqueSize -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\nreturn val\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc pollFirst() -> Int {\npoll(isFront: true)\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc pollLast() -> Int {\npoll(isFront: false)\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -> Int? {\nisEmpty() ? nil : front?.val\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -> Int? {\nisEmpty() ? nil : rear?.val\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -> [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n</code></pre> linkedlist_deque.zig<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/deque/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u4e0e\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u961f\u5217\u7c7b\u4f3c\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u73af\u5f62\u6570\u7ec4\u6765\u5b9e\u73b0\u53cc\u5411\u961f\u5217\u3002\u5728\u5b9e\u73b0\u961f\u5217\u7684\u57fa\u7840\u4e0a\uff0c\u589e\u52a0\u5b9e\u73b0\u201c\u961f\u9996\u5165\u961f\u201d\u548c\u201c\u961f\u5c3e\u51fa\u961f\u201d\u65b9\u6cd5\u5373\u53ef\u3002</p> ArrayDequepushLast()pushFirst()pollLast()pollFirst() <p></p> <p></p> <p></p> <p></p> <p></p> <p>\u4ee5\u4e0b\u662f\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array_deque.java<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\npublic ArrayDeque(int capacity) {\nthis.nums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate int index(int i) {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\nreturn (i + capacity()) % capacity();\n}\n/* \u961f\u9996\u5165\u961f */\npublic void pushFirst(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nfront = index(front - 1);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num;\nqueSize++;\n}\n/* \u961f\u5c3e\u5165\u961f */\npublic void pushLast(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nint rear = index(front + queSize);\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u961f\u9996\u51fa\u961f */\npublic int pollFirst() {\nint num = peekFirst();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(front + 1);\nqueSize--;\nreturn num;\n}\n/* \u961f\u5c3e\u51fa\u961f */\npublic int pollLast() {\nint num = peekLast();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peekFirst() {\nif (isEmpty())\nthrow new EmptyStackException();\nreturn nums[front];\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\npublic int peekLast() {\nif (isEmpty())\nthrow new EmptyStackException();\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nint last = index(front + queSize - 1);\nreturn nums[last];\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[index(j)];\n}\nreturn res;\n}\n}\n</code></pre> array_deque.cpp<pre><code>\n</code></pre> array_deque.py<pre><code>\n</code></pre> array_deque.go<pre><code>\n</code></pre> array_deque.js<pre><code>\n</code></pre> array_deque.ts<pre><code>\n</code></pre> array_deque.c<pre><code>\n</code></pre> array_deque.cs<pre><code>\n</code></pre> array_deque.swift<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n/* \u6784\u9020\u65b9\u6cd5 */\ninit(capacity: Int) {\nnums = Array(repeating: 0, count: capacity)\nfront = 0\nqueSize = 0\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -> Int {\nnums.count\n}\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nprivate func index(i: Int) -> Int {\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n// \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n(i + capacity()) % capacity()\n}\n/* \u961f\u9996\u5165\u961f */\nfunc pushFirst(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\nfront = index(i: front - 1)\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\nnums[front] = num\nqueSize += 1\n}\n/* \u961f\u5c3e\u5165\u961f */\nfunc pushLast(num: Int) {\nif size() == capacity() {\nprint(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\nlet rear = index(i: front + size())\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u961f\u9996\u51fa\u961f */\nfunc pollFirst() -> Int {\nlet num = peekFirst()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\nfront = index(i: front + 1)\nqueSize -= 1\nreturn num\n}\n/* \u961f\u5c3e\u51fa\u961f */\nfunc pollLast() -> Int {\nlet num = peekLast()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peekFirst() -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc peekLast() -> Int {\nif isEmpty() {\nfatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n}\n// \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\nlet last = index(i: front + size() - 1)\nreturn nums[last]\n}\n/* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\nfunc toArray() -> [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: size())\nfor (i, j) in sequence(first: (0, front), next: { $0 < self.size() - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[index(i: j)]\n}\nreturn res\n}\n}\n</code></pre> array_deque.zig<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/queue/","title":"5.2. \u00a0 \u961f\u5217\uff08Queue\uff09","text":"<p>\u300c\u961f\u5217 Queue\u300d\u662f\u4e00\u79cd\u9075\u5faa\u300c\u5148\u5165\u5148\u51fa first in, first out\u300d\u6570\u636e\u64cd\u4f5c\u89c4\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u987e\u540d\u601d\u4e49\uff0c\u961f\u5217\u6a21\u62df\u7684\u662f\u6392\u961f\u73b0\u8c61\uff0c\u5373\u5916\u9762\u7684\u4eba\u4e0d\u65ad\u52a0\u5165\u961f\u5217\u5c3e\u90e8\uff0c\u800c\u5904\u4e8e\u961f\u5217\u5934\u90e8\u7684\u4eba\u4e0d\u65ad\u5730\u79bb\u5f00\u3002</p> <p>\u6211\u4eec\u5c06\u961f\u5217\u5934\u90e8\u79f0\u4e3a\u300c\u961f\u9996\u300d\uff0c\u961f\u5217\u5c3e\u90e8\u79f0\u4e3a\u300c\u961f\u5c3e\u300d\uff0c\u5c06\u628a\u5143\u7d20\u52a0\u5165\u961f\u5c3e\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u5165\u961f\u300d\uff0c\u5220\u9664\u961f\u9996\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u51fa\u961f\u300d\u3002</p> <p></p> <p> Fig. \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\u7528\u64cd\u4f5c\u89c1\u4e0b\u8868\uff0c\u65b9\u6cd5\u540d\u9700\u6839\u636e\u7279\u5b9a\u8bed\u8a00\u6765\u786e\u5b9a\u3002</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)\\) poll() \u961f\u9996\u5143\u7d20\u51fa\u961f \\(O(1)\\) front() \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \\(O(1)\\) size() \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 \\(O(1)\\) isEmpty() \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a \\(O(1)\\) <p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5b9e\u73b0\u597d\u7684\u961f\u5217\u7c7b\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig 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 poll = queue.poll();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.size();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = queue.isEmpty();\n</code></pre> queue.cpp<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\nqueue<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.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 = 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 = que[0];\n\"\"\" \u5143\u7d20\u51fa\u961f \"\"\"\npop = que.popleft()\n\"\"\" \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 \"\"\"\nsize = len(que)\n\"\"\" \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a \"\"\"\nis_empty = len(que) == 0\n</code></pre> queue_test.go<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// \u5728 Go \u4e2d\uff0c\u5c06 list \u4f5c\u4e3a\u961f\u5217\u6765\u4f7f\u7528\nqueue := list.New()\n/* \u5143\u7d20\u5165\u961f */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek := queue.Front()\n/* \u5143\u7d20\u51fa\u961f */\npoll := queue.Front()\nqueue.Remove(poll)\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nsize := queue.Len()\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty := queue.Len() == 0\n</code></pre> queue.js<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// JavaScript \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nconst queue = [];\n/* \u5143\u7d20\u5165\u961f */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nconst peek = queue[0];\n/* \u5143\u7d20\u51fa\u961f */\n// \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u56e0\u6b64 shift() \u65b9\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\nconst poll = 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 poll = queue.shift();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nconst size = queue.length;\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nconst empty = queue.length === 0;\n</code></pre> queue.c<pre><code>\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 poll = queue.Dequeue();\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size = queue.Count();\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = queue.Count() == 0;\n</code></pre> queue.swift<pre><code>/* \u521d\u59cb\u5316\u961f\u5217 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u961f\u5217\u6765\u4f7f\u7528\nvar queue: [Int] = []\n/* \u5143\u7d20\u5165\u961f */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nlet peek = queue.first!\n/* \u5143\u7d20\u51fa\u961f */\n// \u4f7f\u7528 Array \u6a21\u62df\u65f6 poll \u7684\u590d\u6742\u5ea6\u4e3a O(n)\nlet pool = queue.removeFirst()\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nlet size = queue.count\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = queue.isEmpty\n</code></pre> queue.zig<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2. \u00a0 \u961f\u5217\u5b9e\u73b0","text":"<p>\u961f\u5217\u9700\u8981\u4e00\u79cd\u53ef\u4ee5\u5728\u4e00\u7aef\u6dfb\u52a0\uff0c\u5e76\u5728\u53e6\u4e00\u7aef\u5220\u9664\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u94fe\u8868\u6216\u6570\u7ec4\u6765\u5b9e\u73b0\u3002</p>"},{"location":"chapter_stack_and_queue/queue/#_1","title":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"<p>\u6211\u4eec\u5c06\u94fe\u8868\u7684\u300c\u5934\u7ed3\u70b9\u300d\u548c\u300c\u5c3e\u7ed3\u70b9\u300d\u5206\u522b\u770b\u4f5c\u662f\u961f\u9996\u548c\u961f\u5c3e\uff0c\u5e76\u89c4\u5b9a\u961f\u5c3e\u53ea\u53ef\u6dfb\u52a0\u7ed3\u70b9\uff0c\u961f\u9996\u53ea\u53ef\u5220\u9664\u7ed3\u70b9\u3002</p> LinkedListQueuepush()poll() <p></p> <p></p> <p></p> <p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u94fe\u8868\u5b9e\u73b0\u961f\u5217\u7684\u793a\u4f8b\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linkedlist_queue.java<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate ListNode front, rear; // \u5934\u7ed3\u70b9 front \uff0c\u5c3e\u7ed3\u70b9 rear \nprivate int queSize = 0;\npublic LinkedListQueue() {\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\n// \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nListNode node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif (front == null) {\nfront = node;\nrear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\n} else {\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int poll() {\nint num = peek();\n// \u5220\u9664\u5934\u7ed3\u70b9\nfront = front.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new EmptyStackException();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.cpp<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate:\nListNode *front, *rear; // \u5934\u7ed3\u70b9 front \uff0c\u5c3e\u7ed3\u70b9 rear \nint queSize;\npublic:\nLinkedListQueue() {\nfront = nullptr;\nrear = nullptr;\nqueSize = 0;\n}\n~LinkedListQueue() {\ndelete front;\ndelete rear;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\n// \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nListNode* node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif (front == nullptr) {\nfront = node;\nrear = node;\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\nelse {\nrear->next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid poll() {\nint num = peek();\n// \u5220\u9664\u5934\u7ed3\u70b9\nListNode *tmp = front;\nfront = front->next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp; queSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (size() == 0)\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn front->val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector<int> toVector() {\nListNode* node = front;\nvector<int> res(size());\nfor (int i = 0; i < res.size(); i++) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n</code></pre> linkedlist_queue.py<pre><code>\"\"\" \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 \"\"\"\nclass LinkedListQueue:\ndef __init__(self):\nself.__front = None # \u5934\u7ed3\u70b9 front\nself.__rear = None # \u5c3e\u7ed3\u70b9 rear\nself.__size = 0\n\"\"\" \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 \"\"\"\ndef size(self):\nreturn self.__size\n\"\"\" \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a \"\"\"\ndef is_empty(self):\nreturn not self.__front\n\"\"\" \u5165\u961f \"\"\"\ndef push(self, num):\n# \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nnode = ListNode(num)\n# \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif self.__front is None:\nself.__front = node\nself.__rear = node\n# \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\nelse:\nself.__rear.next = node\nself.__rear = node\nself.__size += 1\n\"\"\" \u51fa\u961f \"\"\"\ndef poll(self):\nnum = self.peek()\n# \u5220\u9664\u5934\u7ed3\u70b9\nself.__front = self.__front.next\nself.__size -= 1\nreturn num\n\"\"\" \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \"\"\"\ndef peek(self):\nif self.size() == 0:\nprint(\"\u961f\u5217\u4e3a\u7a7a\")\nreturn False\nreturn self.__front.val\n\"\"\" \u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370 \"\"\"\ndef to_list(self):\nqueue = []\ntemp = self.__front\nwhile temp:\nqueue.append(temp.val)\ntemp = temp.next\nreturn queue\n</code></pre> linkedlist_queue.go<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n// \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\nreturn &linkedListQueue{\ndata: list.New(),\n}\n}\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\ns.data.PushBack(value)\n}\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) poll() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Front()\nreturn e.Value\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\nreturn s.data\n}\n</code></pre> linkedlist_queue.js<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n#front; // \u5934\u7ed3\u70b9 #front\n#rear; // \u5c3e\u7ed3\u70b9 #rear\n#queSize = 0;\nconstructor() {\nthis.#front = null;\nthis.#rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num) {\n// \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nconst node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif (!this.#front) {\nthis.#front = node;\nthis.#rear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\n} else {\nthis.#rear.next = node;\nthis.#rear = node;\n}\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npoll() {\nconst num = this.peek();\n// \u5220\u9664\u5934\u7ed3\u70b9\nthis.#front = this.#front.next;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.size === 0)\nthrow new Error(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn this.#front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#front;\nconst res = new Array(this.size);\nfor (let i = 0; i < res.length; i++) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.ts<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate front: ListNode | null; // \u5934\u7ed3\u70b9 front\nprivate rear: ListNode | null; // \u5c3e\u7ed3\u70b9 rear\nprivate queSize: number = 0;\nconstructor() {\nthis.front = null;\nthis.rear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size === 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\n// \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nconst node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif (!this.front) {\nthis.front = node;\nthis.rear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\n} else {\nthis.rear!.next = node;\nthis.rear = node;\n}\nthis.queSize++;\n}\n/* \u51fa\u961f */\npoll(): number {\nconst num = this.peek();\nif (!this.front)\nthrow new Error(\"\u961f\u5217\u4e3a\u7a7a\")\n// \u5220\u9664\u5934\u7ed3\u70b9\nthis.front = this.front.next;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.size === 0)\nthrow new Error(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn this.front!.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.front;\nconst res = new Array<number>(this.size);\nfor (let i = 0; i < res.length; i++) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.c<pre><code>[class]{linkedListQueue}-[func]{}\n</code></pre> linkedlist_queue.cs<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue\n{\nprivate ListNode? front, rear; // \u5934\u7ed3\u70b9 front \uff0c\u5c3e\u7ed3\u70b9 rear \nprivate int queSize = 0;\npublic LinkedListQueue()\n{\nfront = null;\nrear = null;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size()\n{\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty()\n{\nreturn size() == 0;\n}\n/* \u5165\u961f */\npublic void push(int num)\n{\n// \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nListNode node = new ListNode(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif (front == null)\n{\nfront = node;\nrear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\n}\nelse if (rear != null)\n{\nrear.next = node;\nrear = node;\n}\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int poll()\n{\nint num = peek();\n// \u5220\u9664\u5934\u7ed3\u70b9\nfront = front?.next;\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek()\n{\nif (size() == 0 || front == null)\nthrow new Exception();\nreturn front.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray()\n{\nif (front == null)\nreturn Array.Empty<int>();\nListNode node = front;\nint[] res = new int[size()];\nfor (int i = 0; i < res.Length; i++)\n{\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_queue.swift<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\nprivate var front: ListNode? // \u5934\u7ed3\u70b9\nprivate var rear: ListNode? // \u5c3e\u7ed3\u70b9\nprivate var _size = 0\ninit() {}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\n_size\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\n// \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nlet node = ListNode(x: num)\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif front == nil {\nfront = node\nrear = node\n}\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\nelse {\nrear?.next = node\nrear = node\n}\n_size += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc poll() -> Int {\nlet num = peek()\n// \u5220\u9664\u5934\u7ed3\u70b9\nfront = front?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn front!.val\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nvar node = front\nvar res = Array(repeating: 0, count: size())\nfor i in res.indices {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n</code></pre> linkedlist_queue.zig<pre><code>// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nfront: ?*inc.ListNode(T) = null, // \u5934\u7ed3\u70b9 front\nrear: ?*inc.ListNode(T) = null, // \u5c3e\u7ed3\u70b9 rear\nqueSize: usize = 0, // \u961f\u5217\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.front = null;\nself.rear = null;\nself.queSize = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.queSize;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.front.?.val;\n} // \u5165\u961f\npub fn push(self: *Self, num: T) !void {\n// \u5c3e\u7ed3\u70b9\u540e\u6dfb\u52a0 num\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\n// \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u7ed3\u70b9\u90fd\u6307\u5411\u8be5\u7ed3\u70b9\nif (self.front == null) {\nself.front = node;\nself.rear = node;\n// \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u7ed3\u70b9\u6dfb\u52a0\u5230\u5c3e\u7ed3\u70b9\u540e\n} else {\nself.rear.?.next = node;\nself.rear = node;\n}\nself.queSize += 1;\n} // \u51fa\u961f\npub fn poll(self: *Self) T {\nvar num = self.peek();\n// \u5220\u9664\u5934\u7ed3\u70b9\nself.front = self.front.?.next;\nself.queSize -= 1;\nreturn num;\n} // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.front;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[i] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/queue/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u6570\u7ec4\u7684\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\u4f4e\u4e0b\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4e0b\u8ff0\u7684\u5de7\u5999\u65b9\u6cd5\u6765\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\u3002</p> <p>\u8003\u8651\u501f\u52a9\u4e00\u4e2a\u53d8\u91cf <code>front</code> \u6765\u6307\u5411\u961f\u9996\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u5e76\u7ef4\u62a4\u53d8\u91cf <code>queSize</code> \u6765\u8bb0\u5f55\u961f\u5217\u957f\u5ea6\u3002\u6211\u4eec\u5b9a\u4e49 <code>rear = front + queSize</code> \uff0c\u8be5\u516c\u5f0f\u8ba1\u7b97\u51fa\u6765\u7684 <code>rear</code> \u6307\u5411\u201c\u961f\u5c3e\u5143\u7d20\u7d22\u5f15 \\(+1\\) \u201d\u7684\u4f4d\u7f6e\u3002</p> <p>\u5728\u8be5\u8bbe\u8ba1\u4e0b\uff0c\u6570\u7ec4\u4e2d\u5305\u542b\u5143\u7d20\u7684\u6709\u6548\u533a\u95f4\u4e3a <code>[front, rear - 1]</code> \uff0c\u8fdb\u800c</p> <ul> <li>\u5bf9\u4e8e\u5165\u961f\u64cd\u4f5c\uff0c\u5c06\u8f93\u5165\u5143\u7d20\u8d4b\u503c\u7ed9 <code>rear</code> \u7d22\u5f15\u5904\uff0c\u5e76\u5c06 <code>queSize</code> \u81ea\u589e \\(1\\) \u5373\u53ef\uff1b</li> <li>\u5bf9\u4e8e\u51fa\u961f\u64cd\u4f5c\uff0c\u4ec5\u9700\u5c06 <code>front</code> \u81ea\u589e \\(1\\) \uff0c\u5e76\u5c06 <code>queSize</code> \u81ea\u51cf \\(1\\) \u5373\u53ef\uff1b</li> </ul> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u5165\u961f\u4e0e\u51fa\u961f\u64cd\u4f5c\u90fd\u4ec5\u9700\u5355\u6b21\u64cd\u4f5c\u5373\u53ef\u5b8c\u6210\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(1)\\) \u3002</p> ArrayQueuepush()poll() <p></p> <p></p> <p></p> <p>\u7ec6\u5fc3\u7684\u540c\u5b66\u53ef\u80fd\u4f1a\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff1a\u5728\u4e0d\u65ad\u5165\u961f\u4e0e\u51fa\u961f\u7684\u8fc7\u7a0b\u4e2d\uff0c<code>front</code> \u548c <code>rear</code> \u90fd\u5728\u5411\u53f3\u79fb\u52a8\uff0c\u5728\u5230\u8fbe\u6570\u7ec4\u5c3e\u90e8\u540e\u5c31\u65e0\u6cd5\u7ee7\u7eed\u79fb\u52a8\u4e86\u3002\u4e3a\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6211\u4eec\u8003\u8651\u5c06\u6570\u7ec4\u770b\u4f5c\u662f\u9996\u5c3e\u76f8\u63a5\u7684\uff0c\u8fd9\u6837\u7684\u6570\u7ec4\u88ab\u79f0\u4e3a\u300c\u73af\u5f62\u6570\u7ec4\u300d\u3002</p> <p>\u5bf9\u4e8e\u73af\u5f62\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u4ee4 <code>front</code> \u6216 <code>rear</code> \u5728\u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u76f4\u63a5\u7ed5\u56de\u5230\u6570\u7ec4\u5934\u90e8\u63a5\u7eed\u904d\u5386\u3002\u8fd9\u79cd\u5468\u671f\u6027\u89c4\u5f8b\u53ef\u4ee5\u901a\u8fc7\u300c\u53d6\u4f59\u64cd\u4f5c\u300d\u6765\u5b9e\u73b0\uff0c\u8be6\u60c5\u8bf7\u89c1\u4ee5\u4e0b\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array_queue.java<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity) {\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity() {\nreturn nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num) {\nif (queSize == capacity()) {\nSystem.out.println(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int poll() {\nint num = peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new EmptyStackException();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++) {\nres[i] = nums[j % capacity()];\n}\nreturn res;\n}\n}\n</code></pre> array_queue.cpp<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate:\nint *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nint front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nint queSize; // \u961f\u5217\u957f\u5ea6\nint queCapacity; // \u961f\u5217\u5bb9\u91cf\npublic:\nArrayQueue(int capacity) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = new int[capacity];\nqueCapacity = capacity;\nfront = queSize = 0;\n}\n~ArrayQueue() {\ndelete[] nums;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity() {\nreturn queCapacity;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size() {\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u961f */\nvoid push(int num) {\nif (queSize == queCapacity) {\ncout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\nreturn;\n}\n// \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (front + queSize) % queCapacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\nvoid poll() {\nint num = peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % queCapacity;\nqueSize--;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek() {\nif (empty())\nthrow out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn nums[front];\n}\n/* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\nvector<int> toVector() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvector<int> arr(queSize);\nfor (int i = 0, j = front; i < queSize; i++, j++) {\narr[i] = nums[j % queCapacity];\n}\nreturn arr;\n}\n};\n</code></pre> array_queue.py<pre><code>\"\"\" \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 \"\"\"\nclass ArrayQueue:\ndef __init__(self, size):\nself.__nums = [0] * size # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nself.__front = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nself.__size = 0 # \u961f\u5217\u957f\u5ea6\n\"\"\" \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf \"\"\"\ndef capacity(self):\nreturn len(self.__nums)\n\"\"\" \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 \"\"\"\ndef size(self):\nreturn self.__size\n\"\"\" \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a \"\"\"\ndef is_empty(self):\nreturn self.__size == 0\n\"\"\" \u5165\u961f \"\"\"\ndef push(self, num):\nassert self.__size < self.capacity(), \"\u961f\u5217\u5df2\u6ee1\"\n# \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n# \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nrear = (self.__front + self.__size) % self.capacity()\n# \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.__nums[rear] = num\nself.__size += 1\n\"\"\" \u51fa\u961f \"\"\"\ndef poll(self):\nnum = self.peek()\n# \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nself.__front = (self.__front + 1) % self.capacity()\nself.__size -= 1\nreturn num\n\"\"\" \u8bbf\u95ee\u961f\u9996\u5143\u7d20 \"\"\"\ndef peek(self):\nassert not self.is_empty(), \"\u961f\u5217\u4e3a\u7a7a\"\nreturn self.__nums[self.__front]\n\"\"\" \u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370 \"\"\"\ndef to_list(self):\nres = [0] * self.size()\nj = self.__front\nfor i in range(self.size()):\nres[i] = self.__nums[(j % self.capacity())]\nj += 1\nreturn res\n</code></pre> array_queue.go<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\nnums []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nfront int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize int // \u961f\u5217\u957f\u5ea6\nqueCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\nreturn &arrayQueue{\nnums: make([]int, queCapacity),\nqueCapacity: queCapacity,\nfront: 0,\nqueSize: 0,\n}\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\nreturn q.queSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\nreturn q.queSize == 0\n}\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n// \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\nif q.queSize == q.queCapacity {\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nrear := (q.front + q.queSize) % q.queCapacity\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nq.nums[rear] = num\nq.queSize++\n}\n/* \u51fa\u961f */\nfunc (q *arrayQueue) poll() any {\nnum := q.peek()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nq.front = (q.front + 1) % q.queCapacity\nq.queSize--\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\nif q.isEmpty() {\nreturn nil\n}\nreturn q.nums[q.front]\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\nrear := (q.front + q.queSize)\nif rear >= q.queCapacity {\nrear %= q.queCapacity\nreturn append(q.nums[q.front:], q.nums[:rear]...)\n}\nreturn q.nums[q.front:rear]\n}\n</code></pre> array_queue.js<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n#nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n#front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n#queSize = 0; // \u961f\u5217\u957f\u5ea6\nconstructor(capacity) {\nthis.#nums = new Array(capacity);\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity() {\nreturn this.#nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.#queSize == 0;\n}\n/* \u5165\u961f */\npush(num) {\nif (this.size == this.capacity) {\nconsole.log(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nconst rear = (this.#front + this.size) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.#nums[rear] = num;\nthis.#queSize++;\n}\n/* \u51fa\u961f */\npoll() {\nconst num = this.peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nthis.#front = (this.#front + 1) % this.capacity;\nthis.#queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek() {\nif (this.empty())\nthrow new Error(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn this.#nums[this.#front];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.#front; i < this.size; i++, j++) {\narr[i] = this.#nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n</code></pre> array_queue.ts<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate queSize: number; // \u961f\u5217\u957f\u5ea6\nconstructor(capacity: number) {\nthis.nums = new Array(capacity);\nthis.front = this.queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nget capacity(): number {\nreturn this.nums.length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.queSize == 0;\n}\n/* \u5165\u961f */\npush(num: number): void {\nif (this.size == this.capacity) {\nconsole.log(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nconst rear = (this.front + this.queSize) % this.capacity;\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nthis.nums[rear] = num;\nthis.queSize++;\n}\n/* \u51fa\u961f */\npoll(): number {\nconst num = this.peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nthis.front = (this.front + 1) % this.capacity;\nthis.queSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npeek(): number {\nif (this.empty())\nthrow new Error(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn this.nums[this.front];\n}\n/* \u8fd4\u56de Array */\ntoArray(): number[] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nconst arr = new Array(this.size);\nfor (let i = 0, j = this.front; i < this.size; i++, j++) {\narr[i] = this.nums[j % this.capacity];\n}\nreturn arr;\n}\n}\n</code></pre> array_queue.c<pre><code>[class]{arrayQueue}-[func]{}\n</code></pre> array_queue.cs<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue\n{\nprivate int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate int queSize; // \u961f\u5217\u957f\u5ea6\npublic ArrayQueue(int capacity)\n{\nnums = new int[capacity];\nfront = queSize = 0;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\npublic int capacity()\n{\nreturn nums.Length;\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\npublic int size()\n{\nreturn queSize;\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty()\n{\nreturn queSize == 0;\n}\n/* \u5165\u961f */\npublic void push(int num)\n{\nif (queSize == capacity())\n{\nConsole.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nint rear = (front + queSize) % capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num;\nqueSize++;\n}\n/* \u51fa\u961f */\npublic int poll()\n{\nint num = peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % capacity();\nqueSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\npublic int peek()\n{\nif (isEmpty())\nthrow new Exception();\nreturn nums[front];\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\npublic int[] toArray()\n{\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nint[] res = new int[queSize];\nfor (int i = 0, j = front; i < queSize; i++, j++)\n{\nres[i] = nums[j % this.capacity()];\n}\nreturn res;\n}\n}\n</code></pre> array_queue.swift<pre><code>/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\nprivate var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\nprivate var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nprivate var queSize = 0 // \u961f\u5217\u957f\u5ea6\ninit(capacity: Int) {\n// \u521d\u59cb\u5316\u6570\u7ec4\nnums = Array(repeating: 0, count: capacity)\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nfunc capacity() -> Int {\nnums.count\n}\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nqueSize\n}\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nqueSize == 0\n}\n/* \u5165\u961f */\nfunc push(num: Int) {\nif size() == capacity() {\nprint(\"\u961f\u5217\u5df2\u6ee1\")\nreturn\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nlet rear = (front + queSize) % capacity()\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nnums[rear] = num\nqueSize += 1\n}\n/* \u51fa\u961f */\n@discardableResult\nfunc poll() -> Int {\nlet num = peek()\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nfront = (front + 1) % capacity()\nqueSize -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n}\nreturn nums[front]\n}\n/* \u8fd4\u56de\u6570\u7ec4 */\nfunc toArray() -> [Int] {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = Array(repeating: 0, count: queSize)\nfor (i, j) in sequence(first: (0, front), next: { $0 < self.queSize - 1 ? ($0 + 1, $1 + 1) : nil }) {\nres[i] = nums[j % capacity()]\n}\nreturn res\n}\n}\n</code></pre> array_queue.zig<pre><code>// \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nnums: []T = undefined, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4 \ncap: usize = 0, // \u961f\u5217\u5bb9\u91cf\nfront: usize = 0, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\nqueSize: usize = 0, // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.cap = cap;\nself.nums = try self.mem_allocator.alloc(T, self.cap);\nstd.mem.set(T, self.nums, @as(T, 0));\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\npub fn capacity(self: *Self) usize {\nreturn self.cap;\n}\n// \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.queSize;\n}\n// \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.queSize == 0;\n}\n// \u5165\u961f\npub fn push(self: *Self, num: T) !void {\nif (self.size() == self.capacity()) {\nstd.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\nreturn;\n}\n// \u8ba1\u7b97\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n// \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\uff0c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\nvar rear = (self.front + self.queSize) % self.capacity();\n// \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\nself.nums[rear] = num;\nself.queSize += 1;\n} // \u51fa\u961f\npub fn poll(self: *Self) T {\nvar num = self.peek();\n// \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\nself.front = (self.front + 1) % self.capacity();\nself.queSize -= 1;\nreturn num;\n} // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\nreturn self.nums[self.front];\n} // \u8fd4\u56de\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\n// \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nvar j: usize = self.front;\nwhile (i < self.size()) : ({ i += 1; j += 1; }) {\nres[i] = self.nums[j % self.capacity()];\n}\nreturn res;\n}\n};\n}\n</code></pre> <p>\u4ee5\u4e0a\u5b9e\u73b0\u7684\u961f\u5217\u4ecd\u5b58\u5728\u5c40\u9650\u6027\uff0c\u5373\u957f\u5ea6\u4e0d\u53ef\u53d8\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u95ee\u9898\u5f88\u5bb9\u6613\u89e3\u51b3\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u7ec4\u66ff\u6362\u4e3a\u5217\u8868\uff08\u5373\u52a8\u6001\u6570\u7ec4\uff09\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>"},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3. \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":"<p>\u4e0e\u6808\u7684\u7ed3\u8bba\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002</p>"},{"location":"chapter_stack_and_queue/queue/#524","title":"5.2.4. \u00a0 \u961f\u5217\u5178\u578b\u5e94\u7528","text":"<ul> <li>\u6dd8\u5b9d\u8ba2\u5355\u3002\u8d2d\u7269\u8005\u4e0b\u5355\u540e\uff0c\u8ba2\u5355\u5c31\u88ab\u52a0\u5165\u5230\u961f\u5217\u4e4b\u4e2d\uff0c\u968f\u540e\u7cfb\u7edf\u518d\u6839\u636e\u987a\u5e8f\u4f9d\u6b21\u5904\u7406\u961f\u5217\u4e2d\u7684\u8ba2\u5355\u3002\u5728\u53cc\u5341\u4e00\u65f6\uff0c\u5728\u77ed\u65f6\u95f4\u5185\u4f1a\u4ea7\u751f\u6d77\u91cf\u7684\u8ba2\u5355\uff0c\u5982\u4f55\u5904\u7406\u300c\u9ad8\u5e76\u53d1\u300d\u5219\u662f\u5de5\u7a0b\u5e08\u4eec\u9700\u8981\u91cd\u70b9\u601d\u8003\u7684\u95ee\u9898\u3002</li> <li>\u5404\u79cd\u5f85\u529e\u4e8b\u9879\u3002\u4efb\u4f55\u9700\u8981\u5b9e\u73b0\u201c\u5148\u6765\u540e\u5230\u201d\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6253\u5370\u673a\u7684\u4efb\u52a1\u961f\u5217\u3001\u9910\u5385\u7684\u51fa\u9910\u961f\u5217\u7b49\u7b49\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/stack/","title":"5.1. \u00a0 \u6808\uff08Stack\uff09","text":"<p>\u300c\u6808 Stack\u300d\u662f\u4e00\u79cd\u9075\u5faa\u300c\u5148\u5165\u540e\u51fa first in, last out\u300d\u6570\u636e\u64cd\u4f5c\u89c4\u5219\u7684\u7ebf\u6027\u6570\u636e\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6808\u7c7b\u6bd4\u4e3a\u653e\u5728\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</p> <p>\u201c\u76d8\u5b50\u201d\u662f\u4e00\u79cd\u5f62\u8c61\u6bd4\u55bb\uff0c\u6211\u4eec\u5c06\u76d8\u5b50\u66ff\u6362\u4e3a\u4efb\u610f\u4e00\u79cd\u5143\u7d20\uff08\u4f8b\u5982\u6574\u6570\u3001\u5b57\u7b26\u3001\u5bf9\u8c61\u7b49\uff09\uff0c\u5c31\u5f97\u5230\u4e86\u6808\u6570\u636e\u7ed3\u6784\u3002</p> <p>\u6211\u4eec\u5c06\u8fd9\u4e00\u645e\u5143\u7d20\u7684\u9876\u90e8\u79f0\u4e3a\u300c\u6808\u9876\u300d\uff0c\u5c06\u5e95\u90e8\u79f0\u4e3a\u300c\u6808\u5e95\u300d\uff0c\u5c06\u628a\u5143\u7d20\u6dfb\u52a0\u5230\u6808\u9876\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u5165\u6808\u300d\uff0c\u5c06\u5220\u9664\u6808\u9876\u5143\u7d20\u7684\u64cd\u4f5c\u79f0\u4e3a\u300c\u51fa\u6808\u300d\u3002</p> <p></p> <p> Fig. \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\u89c1\u4e0b\u8868\uff08\u65b9\u6cd5\u547d\u540d\u4ee5 Java \u4e3a\u4f8b\uff09\u3002</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)\\) size() \u83b7\u53d6\u6808\u7684\u957f\u5ea6 \\(O(1)\\) isEmpty() \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a \\(O(1)\\) <p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7f16\u7a0b\u8bed\u8a00\u5b9e\u73b0\u597d\u7684\u6808\u7c7b\u3002 \u67d0\u4e9b\u8bed\u8a00\u5e76\u672a\u4e13\u95e8\u63d0\u4f9b\u6808\u7c7b\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u628a\u8be5\u8bed\u8a00\u7684\u300c\u6570\u7ec4\u300d\u6216\u300c\u94fe\u8868\u300d\u770b\u4f5c\u6808\u6765\u4f7f\u7528\uff0c\u5e76\u901a\u8fc7\u201c\u8111\u8865\u201d\u6765\u5c4f\u853d\u65e0\u5173\u64cd\u4f5c\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig stack.java<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// \u5728 Java \u4e2d\uff0c\u63a8\u8350\u5c06 ArrayList \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nList<Integer> stack = new ArrayList<>();\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.get(stack.size() - 1);\n/* \u5143\u7d20\u51fa\u6808 */\nint pop = stack.remove(stack.size() - 1);\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = stack.isEmpty();\n</code></pre> stack.cpp<pre><code>/* \u521d\u59cb\u5316\u6808 */\nstack<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();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size = stack.size();\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nbool empty = stack.empty();\n</code></pre> stack.py<pre><code>\"\"\" \u521d\u59cb\u5316\u6808 \"\"\"\n# Python \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a List \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nstack = []\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 = stack[-1]\n\"\"\" \u5143\u7d20\u51fa\u6808 \"\"\"\npop = stack.pop()\n\"\"\" \u83b7\u53d6\u6808\u7684\u957f\u5ea6 \"\"\"\nsize = len(stack)\n\"\"\" \u5224\u65ad\u662f\u5426\u4e3a\u7a7a \"\"\"\nis_empty = len(stack) == 0\n</code></pre> stack_test.go<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// \u5728 Go \u4e2d\uff0c\u63a8\u8350\u5c06 Slice \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack []int\n/* \u5143\u7d20\u5165\u6808 */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek := stack[len(stack)-1]\n/* \u5143\u7d20\u51fa\u6808 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nsize := len(stack)\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nisEmpty := len(stack) == 0\n</code></pre> stack.js<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// Javascript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack = [];\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length-1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n</code></pre> stack.ts<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// Typescript \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528 \nconst stack: number[] = [];\n/* \u5143\u7d20\u5165\u6808 */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nconst peek = stack[stack.length - 1];\n/* \u5143\u7d20\u51fa\u6808 */\nconst pop = stack.pop();\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nconst size = stack.length;\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nconst is_empty = stack.length === 0;\n</code></pre> stack.c<pre><code>\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.swift<pre><code>/* \u521d\u59cb\u5316\u6808 */\n// Swift \u6ca1\u6709\u5185\u7f6e\u7684\u6808\u7c7b\uff0c\u53ef\u4ee5\u628a Array \u5f53\u4f5c\u6808\u6765\u4f7f\u7528\nvar stack: [Int] = []\n/* \u5143\u7d20\u5165\u6808 */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nlet peek = stack.last!\n/* \u5143\u7d20\u51fa\u6808 */\nlet pop = stack.removeLast()\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nlet size = stack.count\n/* \u5224\u65ad\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = stack.isEmpty\n</code></pre> stack.zig<pre><code>\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2. \u00a0 \u6808\u7684\u5b9e\u73b0","text":"<p>\u4e3a\u4e86\u66f4\u52a0\u6e05\u6670\u5730\u4e86\u89e3\u6808\u7684\u8fd0\u884c\u673a\u5236\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u81ea\u5df1\u52a8\u624b\u5b9e\u73b0\u4e00\u4e2a\u6808\u7c7b\u3002</p> <p>\u6808\u89c4\u5b9a\u5143\u7d20\u662f\u5148\u5165\u540e\u51fa\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u80fd\u5728\u6808\u9876\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u7136\u800c\uff0c\u6570\u7ec4\u6216\u94fe\u8868\u90fd\u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u5220\u9664\u5143\u7d20\uff0c\u56e0\u6b64 \u6808\u53ef\u88ab\u770b\u4f5c\u662f\u4e00\u79cd\u53d7\u7ea6\u675f\u7684\u6570\u7ec4\u6216\u94fe\u8868\u3002\u6362\u8a00\u4e4b\uff0c\u6211\u4eec\u53ef\u4ee5\u201c\u5c4f\u853d\u201d\u6570\u7ec4\u6216\u94fe\u8868\u7684\u90e8\u5206\u65e0\u5173\u64cd\u4f5c\uff0c\u4f7f\u4e4b\u5bf9\u5916\u7684\u8868\u73b0\u903b\u8f91\u7b26\u5408\u6808\u7684\u89c4\u5b9a\u5373\u53ef\u3002</p>"},{"location":"chapter_stack_and_queue/stack/#_1","title":"\u57fa\u4e8e\u94fe\u8868\u7684\u5b9e\u73b0","text":"<p>\u4f7f\u7528\u300c\u94fe\u8868\u300d\u5b9e\u73b0\u6808\u65f6\uff0c\u5c06\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u770b\u4f5c\u6808\u9876\uff0c\u5c06\u5c3e\u7ed3\u70b9\u770b\u4f5c\u6808\u5e95\u3002</p> <p>\u5bf9\u4e8e\u5165\u6808\u64cd\u4f5c\uff0c\u5c06\u5143\u7d20\u63d2\u5165\u5230\u94fe\u8868\u5934\u90e8\u5373\u53ef\uff0c\u8fd9\u79cd\u7ed3\u70b9\u6dfb\u52a0\u65b9\u5f0f\u88ab\u79f0\u4e3a\u201c\u5934\u63d2\u6cd5\u201d\u3002\u800c\u5bf9\u4e8e\u51fa\u6808\u64cd\u4f5c\uff0c\u5219\u5c06\u5934\u7ed3\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u5373\u53ef\u3002</p> LinkedListStackpush()pop() <p></p> <p></p> <p></p> <p>\u4ee5\u4e0b\u662f\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u6808\u7684\u793a\u4f8b\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig linkedlist_stack.java<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate ListNode stackPeek; // \u5c06\u5934\u7ed3\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0; // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack() {\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop() {\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (size() == 0)\nthrow new EmptyStackException();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray() {\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.cpp<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate:\nListNode* stackTop; // \u5c06\u5934\u7ed3\u70b9\u4f5c\u4e3a\u6808\u9876\nint stkSize; // \u6808\u7684\u957f\u5ea6\npublic:\nLinkedListStack() {\nstackTop = nullptr;\nstkSize = 0;\n}\n~LinkedListStack() {\nfreeMemoryLinkedList(stackTop);\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nListNode* node = new ListNode(num);\nnode->next = stackTop;\nstackTop = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint num = top();\nListNode *tmp = stackTop;\nstackTop = stackTop->next;\n// \u91ca\u653e\u5185\u5b58\ndelete tmp;\nstkSize--;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif (size() == 0)\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stackTop->val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nvector<int> toVector() {\nListNode* node = stackTop;\nvector<int> res(size());\nfor (int i = res.size() - 1; i >= 0; i--) {\nres[i] = node->val;\nnode = node->next;\n}\nreturn res;\n}\n};\n</code></pre> linkedlist_stack.py<pre><code>\"\"\" \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 \"\"\"\nclass LinkedListStack:\ndef __init__(self):\nself.__peek = None\nself.__size = 0\n\"\"\" \u83b7\u53d6\u6808\u7684\u957f\u5ea6 \"\"\"\ndef size(self):\nreturn self.__size\n\"\"\" \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a \"\"\"\ndef is_empty(self):\nreturn not self.__peek\n\"\"\" \u5165\u6808 \"\"\"\ndef push(self, val):\nnode = ListNode(val)\nnode.next = self.__peek\nself.__peek = node\nself.__size += 1\n\"\"\" \u51fa\u6808 \"\"\"\ndef pop(self):\nnum = self.peek()\nself.__peek = self.__peek.next\nself.__size -= 1\nreturn num\n\"\"\" \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \"\"\"\ndef peek(self):\n# \u5224\u7a7a\u5904\u7406\nif not self.__peek: return None\nreturn self.__peek.val\n\"\"\" \u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370 \"\"\"\ndef to_list(self):\narr = []\nnode = self.__peek\nwhile node:\narr.append(node.val)\nnode = node.next\narr.reverse()\nreturn arr\n</code></pre> linkedlist_stack.go<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n// \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\ndata *list.List\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\nreturn &linkedListStack{\ndata: list.New(),\n}\n}\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\ns.data.PushBack(value)\n}\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\ns.data.Remove(e)\nreturn e.Value\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\ne := s.data.Back()\nreturn e.Value\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\nreturn s.data.Len()\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\nreturn s.data.Len() == 0\n}\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\nreturn s.data\n}\n</code></pre> linkedlist_stack.js<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n#stackPeek; // \u5c06\u5934\u7ed3\u70b9\u4f5c\u4e3a\u6808\u9876\n#stkSize = 0; // \u6808\u7684\u957f\u5ea6\nconstructor() {\nthis.#stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.#stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty() {\nreturn this.size == 0;\n}\n/* \u5165\u6808 */\npush(num) {\nconst node = new ListNode(num);\nnode.next = this.#stackPeek;\nthis.#stackPeek = node;\nthis.#stkSize++;\n}\n/* \u51fa\u6808 */\npop() {\nconst num = this.peek();\nthis.#stackPeek = this.#stackPeek.next;\nthis.#stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek() {\nif (!this.#stackPeek)\nthrow new Error(\"\u6808\u4e3a\u7a7a\");\nreturn this.#stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray() {\nlet node = this.#stackPeek;\nconst res = new Array(this.size);\nfor (let i = res.length - 1; i >= 0; i--) {\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.ts<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate stackPeek: ListNode | null; // \u5c06\u5934\u7ed3\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\nconstructor() {\nthis.stackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nisEmpty(): boolean {\nreturn this.size == 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nconst node = new ListNode(num);\nnode.next = this.stackPeek;\nthis.stackPeek = node;\nthis.stkSize++;\n}\n/* \u51fa\u6808 */\npop(): number {\nconst num = this.peek();\nif (!this.stackPeek)\nthrow new Error(\"\u6808\u4e3a\u7a7a\");\nthis.stackPeek = this.stackPeek.next;\nthis.stkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npeek(): number {\nif (!this.stackPeek)\nthrow new Error(\"\u6808\u4e3a\u7a7a\");\nreturn this.stackPeek.val;\n}\n/* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\ntoArray(): number[] {\nlet node = this.stackPeek;\nconst res = new Array<number>(this.size);\nfor (let i = res.length - 1; i >= 0; i--) {\nres[i] = node!.val;\nnode = node!.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.c<pre><code>[class]{linkedListStack}-[func]{}\n</code></pre> linkedlist_stack.cs<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack\n{\nprivate ListNode? stackPeek; // \u5c06\u5934\u7ed3\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate int stkSize = 0; // \u6808\u7684\u957f\u5ea6\npublic LinkedListStack()\n{\nstackPeek = null;\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size()\n{\nreturn stkSize;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty()\n{\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num)\n{\nListNode node = new ListNode(num);\nnode.next = stackPeek;\nstackPeek = node;\nstkSize++;\n}\n/* \u51fa\u6808 */\npublic int pop()\n{\nif (stackPeek == null)\nthrow new Exception();\nint num = peek();\nstackPeek = stackPeek.next;\nstkSize--;\nreturn num;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek()\n{\nif (size() == 0 || stackPeek == null)\nthrow new Exception();\nreturn stackPeek.val;\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray()\n{\nif (stackPeek == null)\nreturn Array.Empty<int>();\nListNode node = stackPeek;\nint[] res = new int[size()];\nfor (int i = res.Length - 1; i >= 0; i--)\n{\nres[i] = node.val;\nnode = node.next;\n}\nreturn res;\n}\n}\n</code></pre> linkedlist_stack.swift<pre><code>/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\nprivate var _peek: ListNode? // \u5c06\u5934\u7ed3\u70b9\u4f5c\u4e3a\u6808\u9876\nprivate var _size = 0 // \u6808\u7684\u957f\u5ea6\ninit() {}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -> Int {\n_size\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nsize() == 0\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nlet node = ListNode(x: num)\nnode.next = _peek\n_peek = node\n_size += 1\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -> Int {\nlet num = peek()\n_peek = _peek?.next\n_size -= 1\nreturn num\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn _peek!.val\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nvar node = _peek\nvar res = Array(repeating: 0, count: _size)\nfor i in sequence(first: res.count - 1, next: { $0 >= 0 + 1 ? $0 - 1 : nil }) {\nres[i] = node!.val\nnode = node?.next\n}\nreturn res\n}\n}\n</code></pre> linkedlist_stack.zig<pre><code>// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstackTop: ?*inc.ListNode(T) = null, // \u5c06\u5934\u7ed3\u70b9\u4f5c\u4e3a\u6808\u9876\nstkSize: usize = 0, // \u6808\u7684\u957f\u5ea6\nmem_arena: ?std.heap.ArenaAllocator = null,\nmem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n// \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) !void {\nif (self.mem_arena == null) {\nself.mem_arena = std.heap.ArenaAllocator.init(allocator);\nself.mem_allocator = self.mem_arena.?.allocator();\n}\nself.stackTop = null;\nself.stkSize = 0;\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.mem_arena == null) return;\nself.mem_arena.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stkSize;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn top(self: *Self) T {\nif (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stackTop.?.val;\n} // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\nvar node = try self.mem_allocator.create(inc.ListNode(T));\nnode.init(num);\nnode.next = self.stackTop;\nself.stackTop = node;\nself.stkSize += 1;\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.top();\nself.stackTop = self.stackTop.?.next;\nself.stkSize -= 1;\nreturn num;\n} // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\npub fn toArray(self: *Self) ![]T {\nvar node = self.stackTop;\nvar res = try self.mem_allocator.alloc(T, self.size());\nstd.mem.set(T, res, @as(T, 0));\nvar i: usize = 0;\nwhile (i < res.len) : (i += 1) {\nres[res.len - i - 1] = node.?.val;\nnode = node.?.next;\n}\nreturn res;\n}\n};\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#_2","title":"\u57fa\u4e8e\u6570\u7ec4\u7684\u5b9e\u73b0","text":"<p>\u4f7f\u7528\u300c\u6570\u7ec4\u300d\u5b9e\u73b0\u6808\u65f6\uff0c\u8003\u8651\u5c06\u6570\u7ec4\u7684\u5c3e\u90e8\u5f53\u4f5c\u6808\u9876\u3002\u8fd9\u6837\u8bbe\u8ba1\u4e0b\uff0c\u300c\u5165\u6808\u300d\u4e0e\u300c\u51fa\u6808\u300d\u64cd\u4f5c\u5c31\u5bf9\u5e94\u5728\u6570\u7ec4\u5c3e\u90e8\u300c\u6dfb\u52a0\u5143\u7d20\u300d\u4e0e\u300c\u5220\u9664\u5143\u7d20\u300d\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4e3a \\(O(1)\\) \u3002</p> ArrayStackpush()pop() <p></p> <p></p> <p></p> <p>\u7531\u4e8e\u5165\u6808\u7684\u5143\u7d20\u53ef\u80fd\u662f\u6e90\u6e90\u4e0d\u65ad\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528\u652f\u6301\u52a8\u6001\u6269\u5bb9\u7684\u300c\u5217\u8868\u300d\uff0c\u8fd9\u6837\u5c31\u65e0\u9700\u81ea\u884c\u5b9e\u73b0\u6570\u7ec4\u6269\u5bb9\u4e86\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig array_stack.java<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate ArrayList<Integer> stack;\npublic ArrayStack() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new ArrayList<>();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic boolean isEmpty() {\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num) {\nstack.add(num);\n}\n/* \u51fa\u6808 */\npublic int pop() {\nif (isEmpty())\nthrow new EmptyStackException();\nreturn stack.remove(size() - 1);\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek() {\nif (isEmpty())\nthrow new EmptyStackException();\nreturn stack.get(size() - 1);\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic Object[] toArray() {\nreturn stack.toArray();\n}\n}\n</code></pre> array_stack.cpp<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate:\nvector<int> stack;\npublic:\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size() {\nreturn stack.size();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool empty() {\nreturn stack.empty();\n}\n/* \u5165\u6808 */\nvoid push(int num) {\nstack.push_back(num);\n}\n/* \u51fa\u6808 */\nvoid pop() {\nint oldTop = top();\nstack.pop_back();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint top() {\nif(empty())\nthrow out_of_range(\"\u6808\u4e3a\u7a7a\");\nreturn stack.back();\n}\n/* \u8fd4\u56de Vector */\nvector<int> toVector() {\nreturn stack;\n}\n};\n</code></pre> array_stack.py<pre><code>\"\"\" \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 \"\"\"\nclass ArrayStack:\ndef __init__(self):\nself.__stack = []\n\"\"\" \u83b7\u53d6\u6808\u7684\u957f\u5ea6 \"\"\"\ndef size(self):\nreturn len(self.__stack)\n\"\"\" \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a \"\"\"\ndef is_empty(self):\nreturn self.__stack == []\n\"\"\" \u5165\u6808 \"\"\"\ndef push(self, item):\nself.__stack.append(item)\n\"\"\" \u51fa\u6808 \"\"\"\ndef pop(self):\nassert not self.is_empty(), \"\u6808\u4e3a\u7a7a\"\nreturn self.__stack.pop()\n\"\"\" \u8bbf\u95ee\u6808\u9876\u5143\u7d20 \"\"\"\ndef peek(self):\nassert not self.is_empty(), \"\u6808\u4e3a\u7a7a\"\nreturn self.__stack[-1]\n\"\"\" \u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370 \"\"\"\ndef to_list(self):\nreturn self.__stack\n</code></pre> array_stack.go<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\ndata []int // \u6570\u636e\n}\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\nreturn &arrayStack{\n// \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\ndata: make([]int, 0, 16),\n}\n}\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\nreturn len(s.data)\n}\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\nreturn s.size() == 0\n}\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n// \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\ns.data = append(s.data, v)\n}\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\nval := s.peek()\ns.data = s.data[:len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\nif s.isEmpty() {\nreturn nil\n}\nval := s.data[len(s.data)-1]\nreturn val\n}\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\nreturn s.data\n}\n</code></pre> array_stack.js<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nstack;\nconstructor() {\nthis.stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size() {\nreturn this.stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty() {\nreturn this.stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num) {\nthis.stack.push(num);\n}\n/* \u51fa\u6808 */\npop() {\nif (this.empty())\nthrow new Error(\"\u6808\u4e3a\u7a7a\");\nreturn this.stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop() {\nif (this.empty())\nthrow new Error(\"\u6808\u4e3a\u7a7a\");\nreturn this.stack[this.stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.stack;\n}\n};\n</code></pre> array_stack.ts<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate stack: number[];\nconstructor() {\nthis.stack = [];\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nget size(): number {\nreturn this.stack.length;\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nempty(): boolean {\nreturn this.stack.length === 0;\n}\n/* \u5165\u6808 */\npush(num: number): void {\nthis.stack.push(num);\n}\n/* \u51fa\u6808 */\npop(): number | undefined {\nif (this.empty())\nthrow new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack.pop();\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\ntop(): number | undefined {\nif (this.empty())\nthrow new Error('\u6808\u4e3a\u7a7a');\nreturn this.stack[this.stack.length - 1];\n}\n/* \u8fd4\u56de Array */\ntoArray() {\nreturn this.stack;\n}\n};\n</code></pre> array_stack.c<pre><code>[class]{arrayStack}-[func]{}\n</code></pre> array_stack.cs<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack\n{\nprivate List<int> stack;\npublic ArrayStack()\n{\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = new();\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\npublic int size()\n{\nreturn stack.Count();\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\npublic bool isEmpty()\n{\nreturn size() == 0;\n}\n/* \u5165\u6808 */\npublic void push(int num)\n{\nstack.Add(num);\n}\n/* \u51fa\u6808 */\npublic int pop()\n{\nif (isEmpty())\nthrow new Exception();\nvar val = peek();\nstack.RemoveAt(size() - 1);\nreturn val;\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\npublic int peek()\n{\nif (isEmpty())\nthrow new Exception();\nreturn stack[size() - 1];\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\npublic int[] toArray()\n{\nreturn stack.ToArray();\n}\n}\n</code></pre> array_stack.swift<pre><code>/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\nprivate var stack: [Int]\ninit() {\n// \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\nstack = []\n}\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc size() -> Int {\nstack.count\n}\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc isEmpty() -> Bool {\nstack.isEmpty\n}\n/* \u5165\u6808 */\nfunc push(num: Int) {\nstack.append(num)\n}\n/* \u51fa\u6808 */\n@discardableResult\nfunc pop() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.removeLast()\n}\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc peek() -> Int {\nif isEmpty() {\nfatalError(\"\u6808\u4e3a\u7a7a\")\n}\nreturn stack.last!\n}\n/* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\nfunc toArray() -> [Int] {\nstack\n}\n}\n</code></pre> array_stack.zig<pre><code>// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\nreturn struct {\nconst Self = @This();\nstack: ?std.ArrayList(T) = null, // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\npub fn init(self: *Self, allocator: std.mem.Allocator) void {\nif (self.stack == null) {\nself.stack = std.ArrayList(T).init(allocator);\n}\n}\n// \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\npub fn deinit(self: *Self) void {\nif (self.stack == null) return;\nself.stack.?.deinit();\n}\n// \u83b7\u53d6\u6808\u7684\u957f\u5ea6\npub fn size(self: *Self) usize {\nreturn self.stack.?.items.len;\n}\n// \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\npub fn isEmpty(self: *Self) bool {\nreturn self.size() == 0;\n}\n// \u8bbf\u95ee\u6808\u9876\u5143\u7d20\npub fn peek(self: *Self) T {\nif (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\nreturn self.stack.?.items[self.size() - 1];\n} // \u5165\u6808\npub fn push(self: *Self, num: T) !void {\ntry self.stack.?.append(num);\n} // \u51fa\u6808\npub fn pop(self: *Self) T {\nvar num = self.stack.?.pop();\nreturn num;\n} // \u8fd4\u56de ArrayList\npub fn toList(self: *Self) std.ArrayList(T) {\nreturn self.stack.?;\n}\n};\n}\n</code></pre>"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3. \u00a0 \u4e24\u79cd\u5b9e\u73b0\u5bf9\u6bd4","text":""},{"location":"chapter_stack_and_queue/stack/#_3","title":"\u652f\u6301\u64cd\u4f5c","text":"<p>\u4e24\u79cd\u5b9e\u73b0\u90fd\u652f\u6301\u6808\u5b9a\u4e49\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\uff0c\u6570\u7ec4\u5b9e\u73b0\u989d\u5916\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u4f46\u8fd9\u5df2\u7ecf\u8d85\u51fa\u6808\u7684\u5b9a\u4e49\u8303\u7574\uff0c\u4e00\u822c\u4e0d\u4f1a\u7528\u5230\u3002</p>"},{"location":"chapter_stack_and_queue/stack/#_4","title":"\u65f6\u95f4\u6548\u7387","text":"<p>\u5728\u6570\u7ec4\uff08\u5217\u8868\uff09\u5b9e\u73b0\u4e2d\uff0c\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u90fd\u662f\u5728\u9884\u5148\u5206\u914d\u597d\u7684\u8fde\u7eed\u5185\u5b58\u4e2d\u64cd\u4f5c\uff0c\u5177\u6709\u5f88\u597d\u7684\u7f13\u5b58\u672c\u5730\u6027\uff0c\u6548\u7387\u5f88\u597d\u3002\u7136\u800c\uff0c\u5982\u679c\u5165\u6808\u65f6\u8d85\u51fa\u6570\u7ec4\u5bb9\u91cf\uff0c\u5219\u4f1a\u89e6\u53d1\u6269\u5bb9\u673a\u5236\uff0c\u90a3\u4e48\u8be5\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\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\u53d8\u6162\u7684\u95ee\u9898\u3002\u7136\u800c\uff0c\u5165\u6808\u64cd\u4f5c\u9700\u8981\u521d\u59cb\u5316\u7ed3\u70b9\u5bf9\u8c61\u5e76\u4fee\u6539\u6307\u9488\uff0c\u56e0\u800c\u6548\u7387\u4e0d\u5982\u6570\u7ec4\u3002\u8fdb\u4e00\u6b65\u5730\u601d\u8003\uff0c\u5982\u679c\u5165\u6808\u5143\u7d20\u4e0d\u662f <code>int</code> \u800c\u662f\u7ed3\u70b9\u5bf9\u8c61\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u7701\u53bb\u521d\u59cb\u5316\u6b65\u9aa4\uff0c\u4ece\u800c\u63d0\u5347\u6548\u7387\u3002</p> <p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u5f53\u5165\u6808\u4e0e\u51fa\u6808\u64cd\u4f5c\u7684\u5143\u7d20\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 <code>int</code> , <code>double</code> \uff09\u65f6\uff0c\u5219\u7ed3\u8bba\u5982\u4e0b\uff1a</p> <ul> <li>\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\u5728\u89e6\u53d1\u6269\u5bb9\u65f6\u4f1a\u53d8\u6162\uff0c\u4f46\u7531\u4e8e\u6269\u5bb9\u662f\u4f4e\u9891\u64cd\u4f5c\uff0c\u56e0\u6b64 \u603b\u4f53\u6548\u7387\u66f4\u9ad8\uff1b</li> <li>\u94fe\u8868\u5b9e\u73b0\u7684\u6808\u53ef\u4ee5\u63d0\u4f9b \u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\uff1b</li> </ul>"},{"location":"chapter_stack_and_queue/stack/#_5","title":"\u7a7a\u95f4\u6548\u7387","text":"<p>\u5728\u521d\u59cb\u5316\u5217\u8868\u65f6\uff0c\u7cfb\u7edf\u4f1a\u7ed9\u5217\u8868\u5206\u914d\u201c\u521d\u59cb\u5bb9\u91cf\u201d\uff0c\u8be5\u5bb9\u91cf\u53ef\u80fd\u8d85\u8fc7\u6211\u4eec\u7684\u9700\u6c42\u3002\u5e76\u4e14\u6269\u5bb9\u673a\u5236\u4e00\u822c\u662f\u6309\u7167\u7279\u5b9a\u500d\u7387\uff08\u6bd4\u5982 2 \u500d\uff09\u8fdb\u884c\u6269\u5bb9\uff0c\u6269\u5bb9\u540e\u7684\u5bb9\u91cf\u4e5f\u53ef\u80fd\u8d85\u51fa\u6211\u4eec\u7684\u9700\u6c42\u3002\u56e0\u6b64\uff0c\u6570\u7ec4\u5b9e\u73b0\u6808\u4f1a\u9020\u6210\u4e00\u5b9a\u7684\u7a7a\u95f4\u6d6a\u8d39\u3002</p> <p>\u5f53\u7136\uff0c\u7531\u4e8e\u7ed3\u70b9\u9700\u8981\u989d\u5916\u5b58\u50a8\u6307\u9488\uff0c\u56e0\u6b64 \u94fe\u8868\u7ed3\u70b9\u6bd4\u6570\u7ec4\u5143\u7d20\u5360\u7528\u66f4\u5927\u3002</p> <p>\u7efc\u4e0a\uff0c\u6211\u4eec\u4e0d\u80fd\u7b80\u5355\u5730\u786e\u5b9a\u54ea\u79cd\u5b9e\u73b0\u66f4\u52a0\u7701\u5185\u5b58\uff0c\u9700\u8981 case-by-case \u5730\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\u5c06\u4e0a\u4e00\u4e2a\u7f51\u9875\u6267\u884c\u5165\u6808\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u300c\u540e\u9000\u300d\u64cd\u4f5c\u6765\u56de\u5230\u4e0a\u4e00\u9875\u9762\uff0c\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\u5219\u9700\u8981\u4e24\u4e2a\u6808\u6765\u914d\u5408\u5b9e\u73b0\u3002</li> <li>\u7a0b\u5e8f\u5185\u5b58\u7ba1\u7406\u3002\u6bcf\u5f53\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u5c31\u4f1a\u5728\u6808\u9876\u6dfb\u52a0\u4e00\u4e2a\u6808\u5e27\uff0c\u7528\u6765\u8bb0\u5f55\u51fd\u6570\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u5411\u4e0b\u9012\u63a8\u4f1a\u4e0d\u65ad\u6267\u884c\u5165\u6808\uff0c\u5411\u4e0a\u56de\u6eaf\u9636\u6bb5\u65f6\u51fa\u6808\u3002</li> </ul>"},{"location":"chapter_stack_and_queue/summary/","title":"5.4. \u00a0 \u5c0f\u7ed3","text":"<ul> <li>\u6808\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u540e\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\u3002</li> <li>\u5728\u65f6\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u5177\u6709\u66f4\u597d\u7684\u5e73\u5747\u6548\u7387\uff0c\u4f46\u6269\u5bb9\u65f6\u4f1a\u5bfc\u81f4\u5355\u6b21\u5165\u6808\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \u3002\u76f8\u5bf9\u5730\uff0c\u6808\u7684\u94fe\u8868\u5b9e\u73b0\u5177\u6709\u66f4\u52a0\u7a33\u5b9a\u7684\u6548\u7387\u8868\u73b0\u3002</li> <li>\u5728\u7a7a\u95f4\u6548\u7387\u65b9\u9762\uff0c\u6808\u7684\u6570\u7ec4\u5b9e\u73b0\u4f1a\u9020\u6210\u4e00\u5b9a\u7a7a\u95f4\u6d6a\u8d39\uff0c\u7136\u800c\u94fe\u8868\u7ed3\u70b9\u6bd4\u6570\u7ec4\u5143\u7d20\u5360\u7528\u5185\u5b58\u66f4\u5927\u3002</li> <li>\u961f\u5217\u662f\u4e00\u79cd\u9075\u5faa\u5148\u5165\u5148\u51fa\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u6216\u94fe\u8868\u5b9e\u73b0\u3002\u5bf9\u4e8e\u4e24\u79cd\u5b9e\u73b0\u7684\u65f6\u95f4\u6548\u7387\u4e0e\u7a7a\u95f4\u6548\u7387\u5bf9\u6bd4\uff0c\u4e0e\u4e0a\u8ff0\u6808\u7684\u7ed3\u8bba\u76f8\u540c\u3002</li> <li>\u53cc\u5411\u961f\u5217\u7684\u4e24\u7aef\u90fd\u53ef\u4ee5\u6dfb\u52a0\u4e0e\u5220\u9664\u5143\u7d20\u3002</li> </ul>"},{"location":"chapter_tree/avl_tree/","title":"7.4. \u00a0 AVL \u6811 *","text":"<p>\u5728\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u7ae0\u8282\u4e2d\u63d0\u5230\uff0c\u5728\u8fdb\u884c\u591a\u6b21\u63d2\u5165\u4e0e\u5220\u9664\u64cd\u4f5c\u540e\uff0c\u4e8c\u53c9\u641c\u7d22\u6811\u53ef\u80fd\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002\u6b64\u65f6\u6240\u6709\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u4f1a\u7531 \\(O(\\log n)\\) \u52a3\u5316\u81f3 \\(O(n)\\) \u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u6267\u884c\u4e24\u6b65\u5220\u9664\u7ed3\u70b9\u540e\uff0c\u8be5\u4e8c\u53c9\u641c\u7d22\u6811\u5c31\u4f1a\u9000\u5316\u4e3a\u94fe\u8868\u3002</p> <p></p> <p> Fig. AVL \u6811\u5728\u5220\u9664\u7ed3\u70b9\u540e\u53d1\u751f\u9000\u5316 </p> <p>\u518d\u6bd4\u5982\uff0c\u5728\u4ee5\u4e0b\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e24\u4e2a\u7ed3\u70b9\u540e\uff0c\u6811\u4e25\u91cd\u5411\u5de6\u504f\u659c\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u968f\u4e4b\u53d1\u751f\u52a3\u5316\u3002</p> <p></p> <p> Fig. AVL \u6811\u5728\u63d2\u5165\u7ed3\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\u63cf\u8ff0\u4e86\u4e00\u7cfb\u5217\u64cd\u4f5c\uff0c\u4f7f\u5f97\u5728\u4e0d\u65ad\u6dfb\u52a0\u4e0e\u5220\u9664\u7ed3\u70b9\u540e\uff0cAVL \u6811\u4ecd\u7136\u4e0d\u4f1a\u53d1\u751f\u9000\u5316\uff0c\u8fdb\u800c\u4f7f\u5f97\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u80fd\u4fdd\u6301\u5728 \\(O(\\log n)\\) \u7ea7\u522b\u3002</p> <p>\u6362\u8a00\u4e4b\uff0c\u5728\u9891\u7e41\u589e\u5220\u67e5\u6539\u7684\u4f7f\u7528\u573a\u666f\u4e2d\uff0cAVL \u6811\u53ef\u59cb\u7ec8\u4fdd\u6301\u5f88\u9ad8\u7684\u6570\u636e\u589e\u5220\u67e5\u6539\u6548\u7387\uff0c\u5177\u6709\u5f88\u597d\u7684\u5e94\u7528\u4ef7\u503c\u3002</p>"},{"location":"chapter_tree/avl_tree/#741-avl","title":"7.4.1. \u00a0 AVL \u6811\u5e38\u89c1\u672f\u8bed","text":"<p>\u300cAVL \u6811\u300d\u65e2\u662f\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u53c8\u662f\u300c\u5e73\u8861\u4e8c\u53c9\u6811\u300d\uff0c\u540c\u65f6\u6ee1\u8db3\u8fd9\u4e24\u79cd\u4e8c\u53c9\u6811\u7684\u6240\u6709\u6027\u8d28\uff0c\u56e0\u6b64\u53c8\u88ab\u79f0\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u3002</p>"},{"location":"chapter_tree/avl_tree/#_1","title":"\u7ed3\u70b9\u9ad8\u5ea6","text":"<p>\u5728 AVL \u6811\u7684\u64cd\u4f5c\u4e2d\uff0c\u9700\u8981\u83b7\u53d6\u7ed3\u70b9\u300c\u9ad8\u5ea6 Height\u300d\uff0c\u6240\u4ee5\u7ed9 AVL \u6811\u7684\u7ed3\u70b9\u7c7b\u6dfb\u52a0 <code>height</code> \u53d8\u91cf\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>/* AVL \u6811\u7ed3\u70b9\u7c7b */\nclass TreeNode {\npublic int val; // \u7ed3\u70b9\u503c\npublic int height; // \u7ed3\u70b9\u9ad8\u5ea6\npublic TreeNode left; // \u5de6\u5b50\u7ed3\u70b9\npublic TreeNode right; // \u53f3\u5b50\u7ed3\u70b9\npublic TreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* AVL \u6811\u7ed3\u70b9\u7c7b */\nstruct TreeNode {\nint val{}; // \u7ed3\u70b9\u503c\nint height = 0; // \u7ed3\u70b9\u9ad8\u5ea6\nTreeNode *left{}; // \u5de6\u5b50\u7ed3\u70b9\nTreeNode *right{}; // \u53f3\u5b50\u7ed3\u70b9\nTreeNode() = default;\nexplicit TreeNode(int x) : val(x){}\n};\n</code></pre> <pre><code>\"\"\" AVL \u6811\u7ed3\u70b9\u7c7b \"\"\"\nclass TreeNode:\ndef __init__(self, val=None, left=None, right=None):\nself.val = val # \u7ed3\u70b9\u503c\nself.height = 0 # \u7ed3\u70b9\u9ad8\u5ea6\nself.left = left # \u5de6\u5b50\u7ed3\u70b9\u5f15\u7528\nself.right = right # \u53f3\u5b50\u7ed3\u70b9\u5f15\u7528\n</code></pre> <pre><code>/* AVL \u6811\u7ed3\u70b9\u7c7b */\ntype TreeNode struct {\nVal int // \u7ed3\u70b9\u503c\nHeight int // \u7ed3\u70b9\u9ad8\u5ea6\nLeft *TreeNode // \u5de6\u5b50\u7ed3\u70b9\u5f15\u7528\nRight *TreeNode // \u53f3\u5b50\u7ed3\u70b9\u5f15\u7528\n}\n</code></pre> <pre><code>class TreeNode {\nval; // \u7ed3\u70b9\u503c\nheight; //\u7ed3\u70b9\u9ad8\u5ea6\nleft; // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nright; // \u53f3\u5b50\u7ed3\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>class TreeNode {\nval: number; // \u7ed3\u70b9\u503c\nheight: number; // \u7ed3\u70b9\u9ad8\u5ea6\nleft: TreeNode | null; // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nright: TreeNode | null; // \u53f3\u5b50\u7ed3\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>\n</code></pre> <pre><code>/* AVL \u6811\u7ed3\u70b9\u7c7b */\nclass TreeNode {\npublic int val; // \u7ed3\u70b9\u503c\npublic int height; // \u7ed3\u70b9\u9ad8\u5ea6\npublic TreeNode? left; // \u5de6\u5b50\u7ed3\u70b9\npublic TreeNode? right; // \u53f3\u5b50\u7ed3\u70b9\npublic TreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* AVL \u6811\u7ed3\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u7ed3\u70b9\u503c\nvar height: Int // \u7ed3\u70b9\u9ad8\u5ea6\nvar left: TreeNode? // \u5de6\u5b50\u7ed3\u70b9\nvar right: TreeNode? // \u53f3\u5b50\u7ed3\u70b9\ninit(x: Int) {\nval = x\nheight = 0\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u300c\u7ed3\u70b9\u9ad8\u5ea6\u300d\u662f\u6700\u8fdc\u53f6\u7ed3\u70b9\u5230\u8be5\u7ed3\u70b9\u7684\u8ddd\u79bb\uff0c\u5373\u8d70\u8fc7\u7684\u300c\u8fb9\u300d\u7684\u6570\u91cf\u3002\u9700\u8981\u7279\u522b\u6ce8\u610f\uff0c\u53f6\u7ed3\u70b9\u7684\u9ad8\u5ea6\u4e3a 0 \uff0c\u7a7a\u7ed3\u70b9\u7684\u9ad8\u5ea6\u4e3a -1\u3002\u6211\u4eec\u5c01\u88c5\u4e24\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u4e0e\u66f4\u65b0\u7ed3\u70b9\u7684\u9ad8\u5ea6\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig avl_tree.java<pre><code>/* \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node == null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 */\nint height(TreeNode* node) {\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node == nullptr ? -1 : node->height;\n}\n/* \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode* node) {\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode->height = max(height(node->left), height(node->right)) + 1;\n}\n</code></pre> avl_tree.py<pre><code>\"\"\" \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 \"\"\"\ndef height(self, node: Optional[TreeNode]) -> int:\n# \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nif node is not None:\nreturn node.height\nreturn -1\n\"\"\" \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 \"\"\"\ndef __update_height(self, node: Optional[TreeNode]):\n# \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = max([self.height(node.left), self.height(node.right)]) + 1\n</code></pre> avl_tree.go<pre><code>/* \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nif node != nil {\nreturn node.Height\n}\nreturn -1\n}\n/* \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\nlh := t.height(node.Left)\nrh := t.height(node.Right)\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nif lh > rh {\nnode.Height = lh + 1\n} else {\nnode.Height = rh + 1\n}\n}\n</code></pre> avl_tree.js<pre><code>/* \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 */\nheight(node) {\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node === null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 */\nupdateHeight(node) {\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node === null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{height}\n[class]{aVLTree}-[func]{updateHeight}\n</code></pre> avl_tree.cs<pre><code>/* \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node)\n{\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn node == null ? -1 : node.height;\n}\n/* \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node)\n{\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.height = Math.Max(height(node.left), height(node.right)) + 1;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -> Int {\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nnode == nil ? -1 : node!.height\n}\n/* \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n</code></pre> avl_tree.zig<pre><code>// \u83b7\u53d6\u7ed3\u70b9\u9ad8\u5ea6\nfn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n_ = self;\n// \u7a7a\u7ed3\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u7ed3\u70b9\u9ad8\u5ea6\u4e3a 0\nreturn if (node == null) -1 else node.?.height;\n}\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nfn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {\n// \u7ed3\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\nnode.?.height = std.math.max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_2","title":"\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50","text":"<p>\u7ed3\u70b9\u7684\u300c\u5e73\u8861\u56e0\u5b50 Balance Factor\u300d\u662f \u7ed3\u70b9\u7684\u5de6\u5b50\u6811\u9ad8\u5ea6\u51cf\u53bb\u53f3\u5b50\u6811\u9ad8\u5ea6\uff0c\u5e76\u5b9a\u4e49\u7a7a\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u4e3a 0 \u3002\u540c\u6837\u5730\uff0c\u6211\u4eec\u5c06\u83b7\u53d6\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig avl_tree.java<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null) return 0;\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - height(node.right);\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode* node) {\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == nullptr) return 0;\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node->left) - height(node->right);\n}\n</code></pre> avl_tree.py<pre><code>\"\"\" \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 \"\"\"\ndef balance_factor(self, node: Optional[TreeNode]) -> int:\n# \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif node is None:\nreturn 0\n# \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.left) - self.height(node.right)\n</code></pre> avl_tree.go<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif node == nil {\nreturn 0\n}\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn t.height(node.Left) - t.height(node.Right)\n}\n</code></pre> avl_tree.js<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node === null) return 0;\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn this.height(node.left) - this.height(node.right);\n}\n</code></pre> avl_tree.ts<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node === null) return 0;\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn this.height(node.left) - this.height(node.right);\n}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{balanceFactor}\n</code></pre> avl_tree.cs<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node)\n{\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null) return 0;\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node.left) - height(node.right);\n}\n</code></pre> avl_tree.swift<pre><code>/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nguard let node = node else { return 0 }\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn height(node: node.left) - height(node: node.right)\n}\n</code></pre> avl_tree.zig<pre><code>// \u83b7\u53d6\u5e73\u8861\u56e0\u5b50\nfn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n// \u7a7a\u7ed3\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\nif (node == null) return 0;\n// \u7ed3\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\nreturn self.height(node.?.left) - self.height(node.?.right);\n}\n</code></pre> <p>Note</p> <p>\u8bbe\u5e73\u8861\u56e0\u5b50\u4e3a \\(f\\) \uff0c\u5219\u4e00\u68f5 AVL \u6811\u7684\u4efb\u610f\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u7686\u6ee1\u8db3 \\(-1 \\le f \\le 1\\) \u3002</p>"},{"location":"chapter_tree/avl_tree/#742-avl","title":"7.4.2. \u00a0 AVL \u6811\u65cb\u8f6c","text":"<p>AVL \u6811\u7684\u72ec\u7279\u4e4b\u5904\u5728\u4e8e\u300c\u65cb\u8f6c Rotation\u300d\u7684\u64cd\u4f5c\uff0c\u5176\u53ef \u5728\u4e0d\u5f71\u54cd\u4e8c\u53c9\u6811\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u524d\u63d0\u4e0b\uff0c\u4f7f\u5931\u8861\u7ed3\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002\u6362\u8a00\u4e4b\uff0c\u65cb\u8f6c\u64cd\u4f5c\u65e2\u53ef\u4ee5\u4f7f\u6811\u4fdd\u6301\u4e3a\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u6811\u91cd\u65b0\u6062\u590d\u4e3a\u300c\u5e73\u8861\u4e8c\u53c9\u6811\u300d\u3002</p> <p>\u6211\u4eec\u5c06\u5e73\u8861\u56e0\u5b50\u7684\u7edd\u5bf9\u503c \\(> 1\\) \u7684\u7ed3\u70b9\u79f0\u4e3a\u300c\u5931\u8861\u7ed3\u70b9\u300d\u3002\u6839\u636e\u7ed3\u70b9\u7684\u5931\u8861\u60c5\u51b5\uff0c\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a \u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u4e00\u8d77\u6765\u770b\u770b\u5b83\u4eec\u662f\u5982\u4f55\u64cd\u4f5c\u7684\u3002</p>"},{"location":"chapter_tree/avl_tree/#case-1-","title":"Case 1 - \u53f3\u65cb","text":"<p>\u5982\u4e0b\u56fe\u6240\u793a\uff08\u7ed3\u70b9\u4e0b\u65b9\u4e3a\u300c\u5e73\u8861\u56e0\u5b50\u300d\uff09\uff0c\u4ece\u5e95\u81f3\u9876\u770b\uff0c\u4e8c\u53c9\u6811\u4e2d\u9996\u4e2a\u5931\u8861\u7ed3\u70b9\u662f \u7ed3\u70b9 3\u3002\u6211\u4eec\u805a\u7126\u5728\u4ee5\u8be5\u5931\u8861\u7ed3\u70b9\u4e3a\u6839\u7ed3\u70b9\u7684\u5b50\u6811\u4e0a\uff0c\u5c06\u8be5\u7ed3\u70b9\u8bb0\u4e3a <code>node</code> \uff0c\u5c06\u5176\u5de6\u5b50\u7ed3\u70b9\u8bb0\u4e3a <code>child</code> \uff0c\u6267\u884c\u300c\u53f3\u65cb\u300d\u64cd\u4f5c\u3002\u5b8c\u6210\u53f3\u65cb\u540e\uff0c\u8be5\u5b50\u6811\u5df2\u7ecf\u6062\u590d\u5e73\u8861\uff0c\u5e76\u4e14\u4ecd\u7136\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811\u3002</p> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p>\u8fdb\u800c\uff0c\u5982\u679c\u7ed3\u70b9 <code>child</code> \u672c\u8eab\u6709\u53f3\u5b50\u7ed3\u70b9\uff08\u8bb0\u4e3a <code>grandChild</code> \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u53f3\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 <code>grandChild</code> \u4f5c\u4e3a <code>node</code> \u7684\u5de6\u5b50\u7ed3\u70b9\u3002</p> <p></p> <p> Fig. \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\u9700\u8981\u901a\u8fc7\u4fee\u6539\u7ed3\u70b9\u6307\u9488\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig avl_tree.java<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\nTreeNode child = node.left;\nTreeNode grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode* rightRotate(TreeNode* node) {\nTreeNode* child = node->left;\nTreeNode* grandChild = child->right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild->right = node;\nnode->left = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.py<pre><code>\"\"\" \u53f3\u65cb\u64cd\u4f5c \"\"\"\ndef __right_rotate(self, node: Optional[TreeNode]) -> TreeNode:\nchild = node.left\ngrand_child = child.right\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node\nnode.left = grand_child\n# \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child\n</code></pre> avl_tree.go<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\nchild := node.Left\ngrandChild := child.Right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.Right = node\nnode.Left = grandChild\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child\n}\n</code></pre> avl_tree.js<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node) {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\nconst child = node.left;\nconst grandChild = child.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{rightRotate}\n</code></pre> avl_tree.cs<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node)\n{\nTreeNode? child = node.left;\nTreeNode? grandChild = child?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.right = node;\nnode.left = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\nlet child = node?.left\nlet grandChild = child?.right\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild?.right = node\nnode?.left = grandChild\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child\n}\n</code></pre> avl_tree.zig<pre><code>// \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.left;\nvar grandChild = child.?.right;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\nchild.?.right = node;\nnode.?.left = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#case-2-","title":"Case 2 - \u5de6\u65cb","text":"<p>\u7c7b\u4f3c\u5730\uff0c\u5982\u679c\u5c06\u53d6\u4e0a\u8ff0\u5931\u8861\u4e8c\u53c9\u6811\u7684\u201c\u955c\u50cf\u201d\uff0c\u90a3\u4e48\u5219\u9700\u8981\u300c\u5de6\u65cb\u300d\u64cd\u4f5c\u3002</p> <p></p> <p> Fig. \u5de6\u65cb\u64cd\u4f5c </p> <p>\u540c\u7406\uff0c\u82e5\u7ed3\u70b9 <code>child</code> \u672c\u8eab\u6709\u5de6\u5b50\u7ed3\u70b9\uff08\u8bb0\u4e3a <code>grandChild</code> \uff09\uff0c\u5219\u9700\u8981\u5728\u300c\u5de6\u65cb\u300d\u4e2d\u6dfb\u52a0\u4e00\u6b65\uff1a\u5c06 <code>grandChild</code> \u4f5c\u4e3a <code>node</code> \u7684\u53f3\u5b50\u7ed3\u70b9\u3002</p> <p></p> <p> Fig. \u6709 grandChild \u7684\u5de6\u65cb\u64cd\u4f5c </p> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u300c\u5de6\u65cb\u300d\u548c\u300c\u53f3\u65cb\u300d\u64cd\u4f5c\u662f\u955c\u50cf\u5bf9\u79f0\u7684\uff0c\u4e24\u8005\u5bf9\u5e94\u89e3\u51b3\u7684\u4e24\u79cd\u5931\u8861\u60c5\u51b5\u4e5f\u662f\u5bf9\u79f0\u7684\u3002\u6839\u636e\u5bf9\u79f0\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u4ece\u300c\u53f3\u65cb\u300d\u63a8\u5bfc\u51fa\u300c\u5de6\u65cb\u300d\u3002\u5177\u4f53\u5730\uff0c\u53ea\u9700\u5c06\u300c\u53f3\u65cb\u300d\u4ee3\u7801\u4e2d\u7684\u628a\u6240\u6709\u7684 <code>left</code> \u66ff\u6362\u4e3a <code>right</code> \u3001\u6240\u6709\u7684 <code>right</code> \u66ff\u6362\u4e3a <code>left</code> \uff0c\u5373\u53ef\u5f97\u5230\u300c\u5de6\u65cb\u300d\u4ee3\u7801\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig avl_tree.java<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\nTreeNode child = node.right;\nTreeNode grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode* leftRotate(TreeNode* node) {\nTreeNode* child = node->right;\nTreeNode* grandChild = child->left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild->left = node;\nnode->right = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.py<pre><code>\"\"\" \u5de6\u65cb\u64cd\u4f5c \"\"\"\ndef __left_rotate(self, node: Optional[TreeNode]) -> TreeNode:\nchild = node.right\ngrand_child = child.left\n# \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node\nnode.right = grand_child\n# \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nself.__update_height(node)\nself.__update_height(child)\n# \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child\n</code></pre> avl_tree.go<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\nchild := node.Right\ngrandChild := child.Left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.Left = node\nnode.Right = grandChild\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\nt.updateHeight(child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child\n}\n</code></pre> avl_tree.js<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node) {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\nconst child = node.right;\nconst grandChild = child.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nthis.updateHeight(node);\nthis.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{leftRotate}\n</code></pre> avl_tree.cs<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node)\n{\nTreeNode? child = node.right;\nTreeNode? grandChild = child?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.left = node;\nnode.right = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node);\nupdateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\nlet child = node?.right\nlet grandChild = child?.left\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild?.left = node\nnode?.right = grandChild\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nupdateHeight(node: node)\nupdateHeight(node: child)\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child\n}\n</code></pre> avl_tree.zig<pre><code>// \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\nvar child = node.?.right;\nvar grandChild = child.?.left;\n// \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\nchild.?.left = node;\nnode.?.right = grandChild;\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nself.updateHeight(node);\nself.updateHeight(child);\n// \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn child;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#case-3-","title":"Case 3 - \u5148\u5de6\u540e\u53f3","text":"<p>\u5bf9\u4e8e\u4e0b\u56fe\u7684\u5931\u8861\u7ed3\u70b9 3 \uff0c\u5355\u4e00\u4f7f\u7528\u5de6\u65cb\u6216\u53f3\u65cb\u90fd\u65e0\u6cd5\u4f7f\u5b50\u6811\u6062\u590d\u5e73\u8861\uff0c\u6b64\u65f6\u9700\u8981\u300c\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u300d\uff0c\u5373\u5148\u5bf9 <code>child</code> \u6267\u884c\u300c\u5de6\u65cb\u300d\uff0c\u518d\u5bf9 <code>node</code> \u6267\u884c\u300c\u53f3\u65cb\u300d\u3002</p> <p></p> <p> Fig. \u5148\u5de6\u65cb\u540e\u53f3\u65cb </p>"},{"location":"chapter_tree/avl_tree/#case-4-","title":"Case 4 - \u5148\u53f3\u540e\u5de6","text":"<p>\u540c\u7406\uff0c\u53d6\u4ee5\u4e0a\u5931\u8861\u4e8c\u53c9\u6811\u7684\u955c\u50cf\uff0c\u5219\u9700\u8981\u300c\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u300d\uff0c\u5373\u5148\u5bf9 <code>child</code> \u6267\u884c\u300c\u53f3\u65cb\u300d\uff0c\u7136\u540e\u5bf9 <code>node</code> \u6267\u884c\u300c\u5de6\u65cb\u300d\u3002</p> <p></p> <p> Fig. \u5148\u53f3\u65cb\u540e\u5de6\u65cb </p>"},{"location":"chapter_tree/avl_tree/#_3","title":"\u65cb\u8f6c\u7684\u9009\u62e9","text":"<p>\u4e0b\u56fe\u63cf\u8ff0\u7684\u56db\u79cd\u5931\u8861\u60c5\u51b5\u4e0e\u4e0a\u8ff0 Cases \u9010\u4e2a\u5bf9\u5e94\uff0c\u5206\u522b\u9700\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> Fig. AVL \u6811\u7684\u56db\u79cd\u65cb\u8f6c\u60c5\u51b5 </p> <p>\u5177\u4f53\u5730\uff0c\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 \u5931\u8861\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50\u3001\u8f83\u9ad8\u4e00\u4fa7\u5b50\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u6765\u786e\u5b9a\u5931\u8861\u7ed3\u70b9\u5c5e\u4e8e\u4e0a\u56fe\u4e2d\u7684\u54ea\u79cd\u60c5\u51b5\u3002</p> \u5931\u8861\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5b50\u7ed3\u70b9\u7684\u5e73\u8861\u56e0\u5b50 \u5e94\u91c7\u7528\u7684\u65cb\u8f6c\u65b9\u6cd5 \\(>0\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(\\geq 0\\) \u53f3\u65cb \\(>0\\) \uff08\u5373\u5de6\u504f\u6811\uff09 \\(<0\\) \u5148\u5de6\u65cb\u540e\u53f3\u65cb \\(<0\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(\\leq 0\\) \u5de6\u65cb \\(<0\\) \uff08\u5373\u53f3\u504f\u6811\uff09 \\(>0\\) \u5148\u53f3\u65cb\u540e\u5de6\u65cb <p>\u4e3a\u65b9\u4fbf\u4f7f\u7528\uff0c\u6211\u4eec\u5c06\u65cb\u8f6c\u64cd\u4f5c\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002\u81f3\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u51fd\u6570\u6765\u65cb\u8f6c\u5404\u79cd\u5931\u8861\u60c5\u51b5\uff0c\u4f7f\u5931\u8861\u7ed3\u70b9\u91cd\u65b0\u6062\u590d\u5e73\u8861\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig avl_tree.java<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode* rotate(TreeNode* node) {\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint _balanceFactor = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (_balanceFactor > 1) {\nif (balanceFactor(node->left) >= 0) {\n// \u53f3\u65cb\nreturn rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode->left = leftRotate(node->left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (_balanceFactor < -1) {\nif (balanceFactor(node->right) <= 0) {\n// \u5de6\u65cb\nreturn leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode->right = rightRotate(node->right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.py<pre><code>\"\"\" \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 \"\"\"\ndef __rotate(self, node: Optional[TreeNode]) -> TreeNode:\n# \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nbalance_factor = self.balance_factor(node)\n# \u5de6\u504f\u6811\nif balance_factor > 1:\nif self.balance_factor(node.left) >= 0:\n# \u53f3\u65cb\nreturn self.__right_rotate(node)\nelse:\n# \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = self.__left_rotate(node.left)\nreturn self.__right_rotate(node)\n# \u53f3\u504f\u6811\nelif balance_factor < -1:\nif self.balance_factor(node.right) <= 0:\n# \u5de6\u65cb\nreturn self.__left_rotate(node)\nelse:\n# \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = self.__right_rotate(node.right)\nreturn self.__left_rotate(node)\n# \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n</code></pre> avl_tree.go<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n// Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\nbf := t.balanceFactor(node)\n// \u5de6\u504f\u6811\nif bf > 1 {\nif t.balanceFactor(node.Left) >= 0 {\n// \u53f3\u65cb\nreturn t.rightRotate(node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.Left = t.leftRotate(node.Left)\nreturn t.rightRotate(node)\n}\n}\n// \u53f3\u504f\u6811\nif bf < -1 {\nif t.balanceFactor(node.Right) <= 0 {\n// \u5de6\u65cb\nreturn t.leftRotate(node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.Right = t.rightRotate(node.Right)\nreturn t.leftRotate(node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n</code></pre> avl_tree.js<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node) {\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (this.balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn this.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.leftRotate(node.left);\nreturn this.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (this.balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn this.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.rightRotate(node.right);\nreturn this.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nconst balanceFactor = this.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactor > 1) {\nif (this.balanceFactor(node.left) >= 0) {\n// \u53f3\u65cb\nreturn this.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = this.leftRotate(node.left);\nreturn this.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactor < -1) {\nif (this.balanceFactor(node.right) <= 0) {\n// \u5de6\u65cb\nreturn this.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = this.rightRotate(node.right);\nreturn this.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{rotate}\n</code></pre> avl_tree.cs<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node)\n{\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nint balanceFactorInt = balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balanceFactorInt > 1)\n{\nif (balanceFactor(node.left) >= 0)\n{\n// \u53f3\u65cb\nreturn rightRotate(node);\n}\nelse\n{\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.left = leftRotate(node?.left);\nreturn rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balanceFactorInt < -1)\n{\nif (balanceFactor(node.right) <= 0)\n{\n// \u5de6\u65cb\nreturn leftRotate(node);\n}\nelse\n{\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.right = rightRotate(node?.right);\nreturn leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nlet balanceFactor = balanceFactor(node: node)\n// \u5de6\u504f\u6811\nif balanceFactor > 1 {\nif self.balanceFactor(node: node?.left) >= 0 {\n// \u53f3\u65cb\nreturn rightRotate(node: node)\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode?.left = leftRotate(node: node?.left)\nreturn rightRotate(node: node)\n}\n}\n// \u53f3\u504f\u6811\nif balanceFactor < -1 {\nif self.balanceFactor(node: node?.right) <= 0 {\n// \u5de6\u65cb\nreturn leftRotate(node: node)\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode?.right = rightRotate(node: node?.right)\nreturn leftRotate(node: node)\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n</code></pre> avl_tree.zig<pre><code>// \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nfn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n// \u83b7\u53d6\u7ed3\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\nvar balance_factor = self.balanceFactor(node);\n// \u5de6\u504f\u6811\nif (balance_factor > 1) {\nif (self.balanceFactor(node.?.left) >= 0) {\n// \u53f3\u65cb\nreturn self.rightRotate(node);\n} else {\n// \u5148\u5de6\u65cb\u540e\u53f3\u65cb\nnode.?.left = self.leftRotate(node.?.left);\nreturn self.rightRotate(node);\n}\n}\n// \u53f3\u504f\u6811\nif (balance_factor < -1) {\nif (self.balanceFactor(node.?.right) <= 0) {\n// \u5de6\u65cb\nreturn self.leftRotate(node);\n} else {\n// \u5148\u53f3\u65cb\u540e\u5de6\u65cb\nnode.?.right = self.rightRotate(node.?.right);\nreturn self.leftRotate(node);\n}\n}\n// \u5e73\u8861\u6811\uff0c\u65e0\u9700\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#743-avl","title":"7.4.3. \u00a0 AVL \u6811\u5e38\u7528\u64cd\u4f5c","text":""},{"location":"chapter_tree/avl_tree/#_4","title":"\u63d2\u5165\u7ed3\u70b9","text":"<p>\u300cAVL \u6811\u300d\u7684\u7ed3\u70b9\u63d2\u5165\u64cd\u4f5c\u4e0e\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u4e3b\u4f53\u7c7b\u4f3c\u3002\u4e0d\u540c\u7684\u662f\uff0c\u5728\u63d2\u5165\u7ed3\u70b9\u540e\uff0c\u4ece\u8be5\u7ed3\u70b9\u5230\u6839\u7ed3\u70b9\u7684\u8def\u5f84\u4e0a\u4f1a\u51fa\u73b0\u4e00\u7cfb\u5217\u300c\u5931\u8861\u7ed3\u70b9\u300d\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u9700\u8981\u4ece\u8be5\u7ed3\u70b9\u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u7ed3\u70b9\u6062\u590d\u5e73\u8861\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig avl_tree.java<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nTreeNode insert(int val) {\nroot = insertHelper(root, val);\nreturn root;\n}\n/* \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\nif (node == null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nTreeNode* insert(int val) {\nroot = insertHelper(root, val);\nreturn root;\n}\n/* \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode* insertHelper(TreeNode* node, int val) {\nif (node == nullptr) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9 */\nif (val < node->val)\nnode->left = insertHelper(node->left, val);\nelse if (val > node->val)\nnode->right = insertHelper(node->right, val);\nelse\nreturn node; // \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n</code></pre> avl_tree.py<pre><code>\"\"\" \u63d2\u5165\u7ed3\u70b9 \"\"\"\ndef insert(self, val) -> TreeNode:\nself.root = self.__insert_helper(self.root, val)\nreturn self.root\n\"\"\" \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\ndef __insert_helper(self, node: Optional[TreeNode], val: int) -> TreeNode:\nif node is None:\nreturn TreeNode(val)\n# 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9\nif val < node.val:\nnode.left = self.__insert_helper(node.left, val)\nelif val > node.val:\nnode.right = self.__insert_helper(node.right, val)\nelse:\n# \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n# \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n</code></pre> avl_tree.go<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nfunc (t *aVLTree) insert(val int) *TreeNode {\nt.root = t.insertHelper(t.root, val)\nreturn t.root\n}\n/* \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn NewTreeNode(val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9 */\nif val < node.Val {\nnode.Left = t.insertHelper(node.Left, val)\n} else if val > node.Val {\nnode.Right = t.insertHelper(node.Right, val)\n} else {\n// \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nreturn node\n}\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node\n}\n</code></pre> avl_tree.js<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\ninsert(val) {\nthis.root = this.insertHelper(this.root, val);\nreturn this.root;\n}\n/* \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node, val) {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9 */\nif (val < node.val) node.left = this.insertHelper(node.left, val);\nelse if (val > node.val) node.right = this.insertHelper(node.right, val);\nelse return node; // \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nthis.updateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\ninsert(val: number): TreeNode {\nthis.root = this.insertHelper(this.root, val);\nreturn this.root;\n}\n/* \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9 */\nif (val < node.val) {\nnode.left = this.insertHelper(node.left, val);\n} else if (val > node.val) {\nnode.right = this.insertHelper(node.right, val);\n} else {\nreturn node; // \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nthis.updateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{insert}\n[class]{aVLTree}-[func]{insertHelper}\n</code></pre> avl_tree.cs<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nTreeNode? insert(int val)\n{\nroot = insertHelper(root, val);\nreturn root;\n}\n/* \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val)\n{\nif (node == null) return new TreeNode(val);\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9 */\nif (val < node.val)\nnode.left = insertHelper(node.left, val);\nelse if (val > node.val)\nnode.right = insertHelper(node.right, val);\nelse\nreturn node; // \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\nupdateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\n@discardableResult\nfunc insert(val: Int) -> TreeNode? {\nroot = insertHelper(node: root, val: val)\nreturn root\n}\n/* \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\nvar node = node\nif node == nil {\nreturn TreeNode(x: val)\n}\n/* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9 */\nif val < node!.val {\nnode?.left = insertHelper(node: node?.left, val: val)\n} else if val > node!.val {\nnode?.right = insertHelper(node: node?.right, val: val)\n} else {\nreturn node // \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nupdateHeight(node: node) // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node\n}\n</code></pre> avl_tree.zig<pre><code>// \u63d2\u5165\u7ed3\u70b9\nfn insert(self: *Self, val: T) !?*inc.TreeNode(T) {\nself.root = try self.insertHelper(self.root, val);\nreturn self.root;\n}\n// \u9012\u5f52\u63d2\u5165\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {\nvar node = node_;\nif (node == null) {\nvar tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\ntmp_node.init(val);\nreturn tmp_node;\n}\n// 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u5e76\u63d2\u5165\u7ed3\u70b9\nif (val < node.?.val) {\nnode.?.left = try self.insertHelper(node.?.left, val);\n} else if (val > node.?.val) {\nnode.?.right = try self.insertHelper(node.?.right, val);\n} else {\nreturn node; // \u91cd\u590d\u7ed3\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n}\nself.updateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n// 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_5","title":"\u5220\u9664\u7ed3\u70b9","text":"<p>\u300cAVL \u6811\u300d\u5220\u9664\u7ed3\u70b9\u64cd\u4f5c\u4e0e\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u5220\u9664\u7ed3\u70b9\u64cd\u4f5c\u603b\u4f53\u76f8\u540c\u3002\u7c7b\u4f3c\u5730\uff0c\u5728\u5220\u9664\u7ed3\u70b9\u540e\uff0c\u4e5f\u9700\u8981\u4ece\u5e95\u81f3\u9876\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6240\u6709\u5931\u8861\u7ed3\u70b9\u6062\u590d\u5e73\u8861\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig avl_tree.java<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nTreeNode remove(int val) {\nroot = removeHelper(root, val);\nreturn root;\n}\n/* \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\nif (node == null) return null;\n/* 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse {\nif (node.left == null || node.right == null) {\nTreeNode child = node.left != null ? node.left : node.right;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = child;\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\nTreeNode temp = getInOrderNext(node.right);\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nTreeNode getInOrderNext(TreeNode node) {\nif (node == null) return node;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (node.left != null) {\nnode = node.left;\n}\nreturn node;\n}\n</code></pre> avl_tree.cpp<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nTreeNode* remove(int val) {\nroot = removeHelper(root, val);\nreturn root;\n}\n/* \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode* removeHelper(TreeNode* node, int val) {\nif (node == nullptr) return nullptr;\n/* 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node->val)\nnode->left = removeHelper(node->left, val);\nelse if (val > node->val)\nnode->right = removeHelper(node->right, val);\nelse {\nif (node->left == nullptr || node->right == nullptr) {\nTreeNode* child = node->left != nullptr ? node->left : node->right;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == nullptr) {\ndelete node;\nreturn nullptr;\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\ndelete node;\nnode = child;\n}\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\nTreeNode* temp = getInOrderNext(node->right);\nnode->right = removeHelper(node->right, temp->val);\nnode->val = temp->val;\n}\n}\nupdateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nTreeNode* getInOrderNext(TreeNode* node) {\nif (node == nullptr) return node;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (node->left != nullptr) {\nnode = node->left;\n}\nreturn node;\n}\n</code></pre> avl_tree.py<pre><code>\"\"\" \u5220\u9664\u7ed3\u70b9 \"\"\"\ndef remove(self, val: int):\nroot = self.__remove_helper(self.root, val)\nreturn root\n\"\"\" \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 \"\"\"\ndef __remove_helper(self, node: Optional[TreeNode], val: int) -> Optional[TreeNode]:\nif node is None:\nreturn None\n# 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif val < node.val:\nnode.left = self.__remove_helper(node.left, val)\nelif val > node.val:\nnode.right = self.__remove_helper(node.right, val)\nelse:\nif node.left is None or node.right is None:\nchild = node.left or node.right\n# \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child is None:\nreturn None\n# \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse:\nnode = child\nelse: # \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\ntemp = self.__get_inorder_next(node.right)\nnode.right = self.__remove_helper(node.right, temp.val)\nnode.val = temp.val\n# \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nself.__update_height(node)\n# 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nreturn self.__rotate(node)\n\"\"\" \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 \"\"\"\ndef __get_inorder_next(self, node: Optional[TreeNode]) -> Optional[TreeNode]:\nif node is None:\nreturn None\n# \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile node.left is not None:\nnode = node.left\nreturn node\n</code></pre> avl_tree.go<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nfunc (t *aVLTree) remove(val int) *TreeNode {\nroot := t.removeHelper(t.root, val)\nreturn root\n}\n/* \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node.Val {\nnode.Left = t.removeHelper(node.Left, val)\n} else if val > node.Val {\nnode.Right = t.removeHelper(node.Right, val)\n} else {\nif node.Left == nil || node.Right == nil {\nchild := node.Left\nif node.Right != nil {\nchild = node.Right\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child == nil {\nreturn nil\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = child\n}\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\ntemp := t.getInOrderNext(node.Right)\nnode.Right = t.removeHelper(node.Right, temp.Val)\nnode.Val = temp.Val\n}\n}\n// \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\nt.updateHeight(node)\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = t.rotate(node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nfunc (t *aVLTree) getInOrderNext(node *TreeNode) *TreeNode {\nif node == nil {\nreturn node\n}\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nfor node.Left != nil {\nnode = node.Left\n}\nreturn node\n}\n</code></pre> avl_tree.js<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nremove(val) {\nthis.root = this.removeHelper(this.root, val);\nreturn this.root;\n}\n/* \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node, val) {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val) node.left = this.removeHelper(node.left, val);\nelse if (val > node.val) node.right = this.removeHelper(node.right, val);\nelse {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) return null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse node = child;\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\nconst temp = this.getInOrderNext(node.right);\nnode.right = this.removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.updateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\ngetInOrderNext(node) {\nif (node === null) return node;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (node.left !== null) {\nnode = node.left;\n}\nreturn node;\n}\n</code></pre> avl_tree.ts<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nremove(val: number): TreeNode {\nthis.root = this.removeHelper(this.root, val);\nreturn this.root;\n}\n/* \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\nif (node === null) return null;\n/* 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val) {\nnode.left = this.removeHelper(node.left, val);\n} else if (val > node.val) {\nnode.right = this.removeHelper(node.right, val);\n} else {\nif (node.left === null || node.right === null) {\nconst child = node.left !== null ? node.left : node.right;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child === null) {\nreturn null;\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nnode = child;\n}\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\nconst temp = this.getInOrderNext(node.right);\nnode.right = this.removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nthis.updateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = this.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\ngetInOrderNext(node: TreeNode): TreeNode {\nif (node === null) return node;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (node.left !== null) {\nnode = node.left;\n}\nreturn node;\n}\n</code></pre> avl_tree.c<pre><code>[class]{aVLTree}-[func]{remove}\n[class]{aVLTree}-[func]{removeHelper}\n[class]{aVLTree}-[func]{getInOrderNext}\n</code></pre> avl_tree.cs<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nTreeNode? remove(int val)\n{\nroot = removeHelper(root, val);\nreturn root;\n}\n/* \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val)\n{\nif (node == null) return null;\n/* 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif (val < node.val)\nnode.left = removeHelper(node.left, val);\nelse if (val > node.val)\nnode.right = removeHelper(node.right, val);\nelse\n{\nif (node.left == null || node.right == null)\n{\nTreeNode? child = node.left != null ? node.left : node.right;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null)\nreturn null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse\nnode = child;\n}\nelse\n{\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\nTreeNode? temp = getInOrderNext(node.right);\nnode.right = removeHelper(node.right, temp.val);\nnode.val = temp.val;\n}\n}\nupdateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nTreeNode? getInOrderNext(TreeNode? node)\n{\nif (node == null) return node;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (node.left != null)\n{\nnode = node.left;\n}\nreturn node;\n}\n</code></pre> avl_tree.swift<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\n@discardableResult\nfunc remove(val: Int) -> TreeNode? {\nroot = removeHelper(node: root, val: val)\nreturn root\n}\n/* \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\nvar node = node\nif node == nil {\nreturn nil\n}\n/* 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b */\nif val < node!.val {\nnode?.left = removeHelper(node: node?.left, val: val)\n} else if val > node!.val {\nnode?.right = removeHelper(node: node?.right, val: val)\n} else {\nif node?.left == nil || node?.right == nil {\nlet child = node?.left != nil ? node?.left : node?.right\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif child == nil {\nreturn nil\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\nelse {\nnode = child\n}\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\nlet temp = getInOrderNext(node: node?.right)\nnode?.right = removeHelper(node: node?.right, val: temp!.val)\nnode?.val = temp!.val\n}\n}\nupdateHeight(node: node) // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n/* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nnode = rotate(node: node)\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nfunc getInOrderNext(node: TreeNode?) -> TreeNode? {\nvar node = node\nif node == nil {\nreturn node\n}\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile node?.left != nil {\nnode = node?.left\n}\nreturn node\n}\n</code></pre> avl_tree.zig<pre><code>// \u5220\u9664\u7ed3\u70b9\nfn remove(self: *Self, val: T) ?*inc.TreeNode(T) {\nself.root = self.removeHelper(self.root, val);\nreturn self.root;\n}\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {\nvar node = node_;\nif (node == null) return null;\n// 1. \u67e5\u627e\u7ed3\u70b9\uff0c\u5e76\u5220\u9664\u4e4b\nif (val < node.?.val) {\nnode.?.left = self.removeHelper(node.?.left, val);\n} else if (val > node.?.val) {\nnode.?.right = self.removeHelper(node.?.right, val);\n} else {\nif (node.?.left == null or node.?.right == null) {\nvar child = if (node.?.left != null) node.?.left else node.?.right;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\nif (child == null) {\nreturn null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n} else {\nnode = child;\n}\n} else {\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u7ed3\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u7ed3\u70b9\u66ff\u6362\u5f53\u524d\u7ed3\u70b9\nvar temp = self.getInOrderNext(node.?.right);\nnode.?.right = self.removeHelper(node.?.right, temp.?.val);\nnode.?.val = temp.?.val;\n}\n}\nself.updateHeight(node); // \u66f4\u65b0\u7ed3\u70b9\u9ad8\u5ea6\n// 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nnode = self.rotate(node);\n// \u8fd4\u56de\u5b50\u6811\u7684\u6839\u7ed3\u70b9\nreturn node;\n}\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09\nfn getInOrderNext(self: *Self, node_: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n_ = self;\nvar node = node_;\nif (node == null) return node;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (node.?.left != null) {\nnode = node.?.left;\n}\nreturn node;\n}\n</code></pre>"},{"location":"chapter_tree/avl_tree/#_6","title":"\u67e5\u627e\u7ed3\u70b9","text":"<p>\u300cAVL \u6811\u300d\u7684\u7ed3\u70b9\u67e5\u627e\u64cd\u4f5c\u4e0e\u300c\u4e8c\u53c9\u641c\u7d22\u6811\u300d\u4e00\u81f4\uff0c\u5728\u6b64\u4e0d\u518d\u8d58\u8ff0\u3002</p>"},{"location":"chapter_tree/avl_tree/#744-avl","title":"7.4.4. \u00a0 AVL \u6811\u5178\u578b\u5e94\u7528","text":"<ul> <li>\u7ec4\u7ec7\u5b58\u50a8\u5927\u578b\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9ad8\u9891\u67e5\u627e\u3001\u4f4e\u9891\u589e\u5220\u573a\u666f\uff1b</li> <li>\u7528\u4e8e\u5efa\u7acb\u6570\u636e\u5e93\u4e2d\u7684\u7d22\u5f15\u7cfb\u7edf\uff1b</li> </ul> <p>\u4e3a\u4ec0\u4e48\u7ea2\u9ed1\u6811\u6bd4 AVL \u6811\u66f4\u53d7\u6b22\u8fce\uff1f</p> <p>\u7ea2\u9ed1\u6811\u7684\u5e73\u8861\u6761\u4ef6\u76f8\u5bf9\u5bbd\u677e\uff0c\u56e0\u6b64\u5728\u7ea2\u9ed1\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9\u6240\u9700\u7684\u65cb\u8f6c\u64cd\u4f5c\u76f8\u5bf9\u66f4\u5c11\uff0c\u7ed3\u70b9\u589e\u5220\u64cd\u4f5c\u76f8\u6bd4 AVL \u6811\u7684\u6548\u7387\u66f4\u9ad8\u3002</p>"},{"location":"chapter_tree/binary_search_tree/","title":"7.3. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811","text":"<p>\u300c\u4e8c\u53c9\u641c\u7d22\u6811 Binary Search Tree\u300d\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff1a</p> <ol> <li>\u5bf9\u4e8e\u6839\u7ed3\u70b9\uff0c\u5de6\u5b50\u6811\u4e2d\u6240\u6709\u7ed3\u70b9\u7684\u503c \\(<\\) \u6839\u7ed3\u70b9\u7684\u503c \\(<\\) \u53f3\u5b50\u6811\u4e2d\u6240\u6709\u7ed3\u70b9\u7684\u503c\uff1b</li> <li>\u4efb\u610f\u7ed3\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u4e5f\u662f\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5373\u4e5f\u6ee1\u8db3\u6761\u4ef6 <code>1.</code> \uff1b</li> </ol> <p></p> <p> Fig. \u4e8c\u53c9\u641c\u7d22\u6811 </p>"},{"location":"chapter_tree/binary_search_tree/#731","title":"7.3.1. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u64cd\u4f5c","text":""},{"location":"chapter_tree/binary_search_tree/#_1","title":"\u67e5\u627e\u7ed3\u70b9","text":"<p>\u7ed9\u5b9a\u76ee\u6807\u7ed3\u70b9\u503c <code>num</code> \uff0c\u53ef\u4ee5\u6839\u636e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6027\u8d28\u6765\u67e5\u627e\u3002\u6211\u4eec\u58f0\u660e\u4e00\u4e2a\u7ed3\u70b9 <code>cur</code> \uff0c\u4ece\u4e8c\u53c9\u6811\u7684\u6839\u7ed3\u70b9 <code>root</code> \u51fa\u53d1\uff0c\u5faa\u73af\u6bd4\u8f83\u7ed3\u70b9\u503c <code>cur.val</code> \u548c <code>num</code> \u4e4b\u95f4\u7684\u5927\u5c0f\u5173\u7cfb</p> <ul> <li>\u82e5 <code>cur.val < num</code> \uff0c\u8bf4\u660e\u76ee\u6807\u7ed3\u70b9\u5728 <code>cur</code> \u7684\u53f3\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c <code>cur = cur.right</code> \uff1b</li> <li>\u82e5 <code>cur.val > num</code> \uff0c\u8bf4\u660e\u76ee\u6807\u7ed3\u70b9\u5728 <code>cur</code> \u7684\u5de6\u5b50\u6811\u4e2d\uff0c\u56e0\u6b64\u6267\u884c <code>cur = cur.left</code> \uff1b</li> <li>\u82e5 <code>cur.val = num</code> \uff0c\u8bf4\u660e\u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\u5e76\u8fd4\u56de\u8be5\u7ed3\u70b9\u5373\u53ef\uff1b</li> </ul> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p>\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u67e5\u627e\u64cd\u4f5c\u548c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u5982\u51fa\u4e00\u8f99\uff0c\u4e5f\u662f\u5728\u6bcf\u8f6e\u6392\u9664\u4e00\u534a\u60c5\u51b5\u3002\u5faa\u73af\u6b21\u6570\u6700\u591a\u4e3a\u4e8c\u53c9\u6811\u7684\u9ad8\u5ea6\uff0c\u5f53\u4e8c\u53c9\u6811\u5e73\u8861\u65f6\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_search_tree.java<pre><code>/* \u67e5\u627e\u7ed3\u70b9 */\nTreeNode search(int num) {\nTreeNode cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num) cur = cur.left;\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse break;\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.cpp<pre><code>/* \u67e5\u627e\u7ed3\u70b9 */\nTreeNode* search(int num) {\nTreeNode* cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num) cur = cur->right;\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur->val > num) cur = cur->left;\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse break;\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.py<pre><code>\"\"\" \u67e5\u627e\u7ed3\u70b9 \"\"\"\ndef search(self, num: int) -> Optional[TreeNode]:\ncur = self.root\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile cur is not None:\n# \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelif cur.val > num:\ncur = cur.left\n# \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse:\nbreak\nreturn cur\n</code></pre> binary_search_tree.go<pre><code>/* \u67e5\u627e\u7ed3\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\nnode := bst.root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nfor node != nil {\nif node.Val < num {\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nnode = node.Right\n} else if node.Val > num {\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nnode = node.Left\n} else {\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn node\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u67e5\u627e\u7ed3\u70b9 */\nfunction search(num) {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num) cur = cur.left;\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse break;\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u67e5\u627e\u7ed3\u70b9 */\nfunction search(num: number): TreeNode | null {\nlet cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val < num) {\ncur = cur.right; // \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else if (cur.val > num) {\ncur = cur.left; // \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\nbreak; // \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.c<pre><code>[class]{binarySearchTree}-[func]{search}\n</code></pre> binary_search_tree.cs<pre><code>/* \u67e5\u627e\u7ed3\u70b9 */\nTreeNode? search(int num)\n{\nTreeNode? cur = root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null)\n{\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if (cur.val > num) cur = cur.left;\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse break;\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn cur;\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u67e5\u627e\u7ed3\u70b9 */\nfunc search(num: Int) -> TreeNode? {\nvar cur = root\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse if cur!.val > num {\ncur = cur?.left\n}\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nelse {\nbreak\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn cur\n}\n</code></pre> binary_search_tree.zig<pre><code>// \u67e5\u627e\u7ed3\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\nvar cur = self.root;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u76ee\u6807\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else if (cur.?.val > num) {\ncur = cur.?.left;\n// \u627e\u5230\u76ee\u6807\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n} else {\nbreak;\n}\n}\n// \u8fd4\u56de\u76ee\u6807\u7ed3\u70b9\nreturn cur;\n}\n</code></pre>"},{"location":"chapter_tree/binary_search_tree/#_2","title":"\u63d2\u5165\u7ed3\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\u7ed3\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\uff0c\u63d2\u5165\u64cd\u4f5c\u5206\u4e3a\u4e24\u6b65\uff1a</p> <ol> <li>\u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff1a\u4e0e\u67e5\u627e\u64cd\u4f5c\u7c7b\u4f3c\uff0c\u6211\u4eec\u4ece\u6839\u7ed3\u70b9\u51fa\u53d1\uff0c\u6839\u636e\u5f53\u524d\u7ed3\u70b9\u503c\u548c <code>num</code> \u7684\u5927\u5c0f\u5173\u7cfb\u5faa\u73af\u5411\u4e0b\u641c\u7d22\uff0c\u76f4\u5230\u8d8a\u8fc7\u53f6\u7ed3\u70b9\uff08\u904d\u5386\u5230 \\(\\text{null}\\) \uff09\u65f6\u8df3\u51fa\u5faa\u73af\uff1b</li> <li>\u5728\u8be5\u4f4d\u7f6e\u63d2\u5165\u7ed3\u70b9\uff1a\u521d\u59cb\u5316\u7ed3\u70b9 <code>num</code> \uff0c\u5c06\u8be5\u7ed3\u70b9\u653e\u5230 \\(\\text{null}\\) \u7684\u4f4d\u7f6e \uff1b</li> </ol> <p>\u4e8c\u53c9\u641c\u7d22\u6811\u4e0d\u5141\u8bb8\u5b58\u5728\u91cd\u590d\u7ed3\u70b9\uff0c\u5426\u5219\u5c06\u4f1a\u8fdd\u80cc\u5176\u5b9a\u4e49\u3002\u56e0\u6b64\u82e5\u5f85\u63d2\u5165\u7ed3\u70b9\u5728\u6811\u4e2d\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4e0d\u6267\u884c\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\u5373\u53ef\u3002</p> <p></p> <p> Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u7ed3\u70b9 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_search_tree.java<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nTreeNode insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null) return null;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num) return null;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u63d2\u5165\u7ed3\u70b9 val\nTreeNode node = new TreeNode(num);\nif (pre.val < num) pre.right = node;\nelse pre.left = node;\nreturn node;\n}\n</code></pre> binary_search_tree.cpp<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nTreeNode* insert(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == nullptr) return nullptr;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur->val == num) return nullptr;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num) cur = cur->right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur->left;\n}\n// \u63d2\u5165\u7ed3\u70b9 val\nTreeNode* node = new TreeNode(num);\nif (pre->val < num) pre->right = node;\nelse pre->left = node;\nreturn node;\n}\n</code></pre> binary_search_tree.py<pre><code>\"\"\" \u63d2\u5165\u7ed3\u70b9 \"\"\"\ndef insert(self, num: int) -> Optional[TreeNode]:\nroot = self.root\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root is None:\nreturn None\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\ncur, pre = root, None\nwhile cur is not None:\n# \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur.val == num:\nreturn None\npre = cur\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur.val < num:\ncur = cur.right\n# \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse:\ncur = cur.left\n# \u63d2\u5165\u7ed3\u70b9 val\nnode = TreeNode(num)\nif pre.val < num:\npre.right = node\nelse:\npre.left = node\nreturn node\n</code></pre> binary_search_tree.go<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) *TreeNode {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn nil\n}\n// \u5f85\u63d2\u5165\u7ed3\u70b9\u4e4b\u524d\u7684\u7ed3\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nreturn nil\n}\npre = cur\nif cur.Val < num {\ncur = cur.Right\n} else {\ncur = cur.Left\n}\n}\n// \u63d2\u5165\u7ed3\u70b9\nnode := NewTreeNode(num)\nif pre.Val < num {\npre.Right = node\n} else {\npre.Left = node\n}\nreturn cur\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nfunction insert(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return null;\nlet cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val === num) return null;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u63d2\u5165\u7ed3\u70b9 val\nlet node = new Tree.TreeNode(num);\nif (pre.val < num) pre.right = node;\nelse pre.left = node;\nreturn node;\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nfunction insert(num: number): TreeNode | null {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn null;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\nif (cur.val === num) {\nreturn null; // \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n}\npre = cur;\nif (cur.val < num) {\ncur = cur.right as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u63d2\u5165\u7ed3\u70b9 val\nlet node = new TreeNode(num);\nif (pre!.val < num) {\npre!.right = node;\n} else {\npre!.left = node;\n}\nreturn node;\n}\n</code></pre> binary_search_tree.c<pre><code>[class]{binarySearchTree}-[func]{insert}\n</code></pre> binary_search_tree.cs<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nTreeNode? insert(int num)\n{\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null) return null;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null)\n{\n// \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.val == num) return null;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u63d2\u5165\u7ed3\u70b9 val\nTreeNode node = new TreeNode(num);\nif (pre != null)\n{\nif (pre.val < num) pre.right = node;\nelse pre.left = node;\n}\nreturn node;\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u63d2\u5165\u7ed3\u70b9 */\nfunc insert(num: Int) -> TreeNode? {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn nil\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif cur!.val == num {\nreturn nil\n}\npre = cur\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u63d2\u5165\u7ed3\u70b9 val\nlet node = TreeNode(x: num)\nif pre!.val < num {\npre?.right = node\n} else {\npre?.left = node\n}\nreturn node\n}\n</code></pre> binary_search_tree.zig<pre><code>// \u63d2\u5165\u7ed3\u70b9\nfn insert(self: *Self, num: T) !?*inc.TreeNode(T) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (self.root == null) return null;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u91cd\u590d\u7ed3\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\nif (cur.?.val == num) return null;\npre = cur;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u63d2\u5165\u7ed3\u70b9 val\nvar node = try self.mem_allocator.create(inc.TreeNode(T));\nnode.init(num);\nif (pre.?.val < num) {\npre.?.right = node;\n} else {\npre.?.left = node;\n}\nreturn node;\n}\n</code></pre> <p>\u4e3a\u4e86\u63d2\u5165\u7ed3\u70b9\uff0c\u9700\u8981\u501f\u52a9 \u8f85\u52a9\u7ed3\u70b9 <code>pre</code> \u4fdd\u5b58\u4e0a\u4e00\u8f6e\u5faa\u73af\u7684\u7ed3\u70b9\uff0c\u8fd9\u6837\u5728\u904d\u5386\u5230 \\(\\text{null}\\) \u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u83b7\u53d6\u5230\u5176\u7236\u7ed3\u70b9\uff0c\u4ece\u800c\u5b8c\u6210\u7ed3\u70b9\u63d2\u5165\u64cd\u4f5c\u3002</p> <p>\u4e0e\u67e5\u627e\u7ed3\u70b9\u76f8\u540c\uff0c\u63d2\u5165\u7ed3\u70b9\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\u3002</p>"},{"location":"chapter_tree/binary_search_tree/#_3","title":"\u5220\u9664\u7ed3\u70b9","text":"<p>\u4e0e\u63d2\u5165\u7ed3\u70b9\u4e00\u6837\uff0c\u6211\u4eec\u9700\u8981\u5728\u5220\u9664\u64cd\u4f5c\u540e\u7ef4\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u201c\u5de6\u5b50\u6811 < \u6839\u7ed3\u70b9 < \u53f3\u5b50\u6811\u201d\u7684\u6027\u8d28\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u4e8c\u53c9\u6811\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u83b7\u53d6\u5f85\u5220\u9664\u7ed3\u70b9\u3002\u63a5\u4e0b\u6765\uff0c\u6839\u636e\u5f85\u5220\u9664\u7ed3\u70b9\u7684\u5b50\u7ed3\u70b9\u6570\u91cf\uff0c\u5220\u9664\u64cd\u4f5c\u9700\u8981\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a</p> <p>\u5f53\u5f85\u5220\u9664\u7ed3\u70b9\u7684\u5b50\u7ed3\u70b9\u6570\u91cf \\(= 0\\) \u65f6\uff0c\u8868\u660e\u5f85\u5220\u9664\u7ed3\u70b9\u662f\u53f6\u7ed3\u70b9\uff0c\u76f4\u63a5\u5220\u9664\u5373\u53ef\u3002</p> <p></p> <p> Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u7ed3\u70b9\uff08\u5ea6\u4e3a 0\uff09 </p> <p>\u5f53\u5f85\u5220\u9664\u7ed3\u70b9\u7684\u5b50\u7ed3\u70b9\u6570\u91cf \\(= 1\\) \u65f6\uff0c\u5c06\u5f85\u5220\u9664\u7ed3\u70b9\u66ff\u6362\u4e3a\u5176\u5b50\u7ed3\u70b9\u5373\u53ef\u3002</p> <p></p> <p> Fig. \u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u5220\u9664\u7ed3\u70b9\uff08\u5ea6\u4e3a 1\uff09 </p> <p>\u5f53\u5f85\u5220\u9664\u7ed3\u70b9\u7684\u5b50\u7ed3\u70b9\u6570\u91cf \\(= 2\\) \u65f6\uff0c\u5220\u9664\u64cd\u4f5c\u5206\u4e3a\u4e09\u6b65\uff1a</p> <ol> <li>\u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\u5728 \u4e2d\u5e8f\u904d\u5386\u5e8f\u5217 \u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff0c\u8bb0\u4e3a <code>nex</code> \uff1b</li> <li>\u5728\u6811\u4e2d\u9012\u5f52\u5220\u9664\u7ed3\u70b9 <code>nex</code> \uff1b</li> <li>\u4f7f\u7528 <code>nex</code> \u66ff\u6362\u5f85\u5220\u9664\u7ed3\u70b9\uff1b</li> </ol> <1><2><3><4> <p></p> <p></p> <p></p> <p></p> <p>\u5220\u9664\u7ed3\u70b9\u64cd\u4f5c\u4e5f\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d\u67e5\u627e\u5f85\u5220\u9664\u7ed3\u70b9 \\(O(\\log n)\\) \uff0c\u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u540e\u7ee7\u7ed3\u70b9 \\(O(\\log n)\\) \u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_search_tree.java<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nTreeNode remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null) return null;\nTreeNode cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num) break;\npre = cur;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null) return null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null) {\n// \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u7ed3\u70b9\nTreeNode child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u7ed3\u70b9 cur\nif (pre.left == cur) pre.left = child;\nelse pre.right = child;\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nTreeNode nex = getInOrderNext(cur.right);\nint tmp = nex.val;\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\nremove(nex.val);\n// \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur.val = tmp;\n}\nreturn cur;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nTreeNode getInOrderNext(TreeNode root) {\nif (root == null) return root;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (root.left != null) {\nroot = root.left;\n}\nreturn root;\n}\n</code></pre> binary_search_tree.cpp<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nTreeNode* remove(int num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == nullptr) return nullptr;\nTreeNode *cur = root, *pre = nullptr;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != nullptr) {\n// \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur->val == num) break;\npre = cur;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur->val < num) cur = cur->right;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur->left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == nullptr) return nullptr;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif (cur->left == nullptr || cur->right == nullptr) {\n// \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u7ed3\u70b9\nTreeNode* child = cur->left != nullptr ? cur->left : cur->right;\n// \u5220\u9664\u7ed3\u70b9 cur\nif (pre->left == cur) pre->left = child;\nelse pre->right = child;\n// \u91ca\u653e\u5185\u5b58\ndelete cur;\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nTreeNode* nex = getInOrderNext(cur->right);\nint tmp = nex->val;\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\nremove(nex->val);\n// \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur->val = tmp;\n}\nreturn cur;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nTreeNode* getInOrderNext(TreeNode* root) {\nif (root == nullptr) return root;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (root->left != nullptr) {\nroot = root->left;\n}\nreturn root;\n}\n</code></pre> binary_search_tree.py<pre><code>\"\"\" \u5220\u9664\u7ed3\u70b9 \"\"\"\ndef remove(self, num: int) -> Optional[TreeNode]:\nroot = self.root\n# \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root is None:\nreturn None\n# \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\ncur, pre = root, None\nwhile cur is not None:\n# \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur.val == num:\nbreak\npre = cur\nif cur.val < num: # \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\ncur = cur.right\nelse: # \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\ncur = cur.left\n# \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur is None:\nreturn None\n# \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif cur.left is None or cur.right is None:\n# \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u7ed3\u70b9\nchild = cur.left or cur.right\n# \u5220\u9664\u7ed3\u70b9 cur\nif pre.left == cur:\npre.left = child\nelse:\npre.right = child\n# \u5b50\u7ed3\u70b9\u6570\u91cf = 2\nelse:\n# \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nnex = self.get_inorder_next(cur.right)\ntmp = nex.val\n# \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\nself.remove(nex.val)\n# \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur.val = tmp\nreturn cur\n\"\"\" \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 \"\"\"\ndef get_inorder_next(self, root: Optional[TreeNode]) -> Optional[TreeNode]:\nif root is None:\nreturn root\n# \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile root.left is not None:\nroot = root.left\nreturn root\n</code></pre> binary_search_tree.go<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) *TreeNode {\ncur := bst.root\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif cur == nil {\nreturn nil\n}\n// \u5f85\u5220\u9664\u7ed3\u70b9\u4e4b\u524d\u7684\u7ed3\u70b9\u4f4d\u7f6e\nvar pre *TreeNode = nil\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nfor cur != nil {\nif cur.Val == num {\nbreak\n}\npre = cur\nif cur.Val < num {\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\ncur = cur.Right\n} else {\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\ncur = cur.Left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn nil\n}\n// \u5b50\u7ed3\u70b9\u6570\u4e3a 0 \u6216 1\nif cur.Left == nil || cur.Right == nil {\nvar child *TreeNode = nil\n// \u53d6\u51fa\u5f85\u5220\u9664\u7ed3\u70b9\u7684\u5b50\u7ed3\u70b9\nif cur.Left != nil {\nchild = cur.Left\n} else {\nchild = cur.Right\n}\n// \u5c06\u5b50\u7ed3\u70b9\u66ff\u6362\u4e3a\u5f85\u5220\u9664\u7ed3\u70b9\nif pre.Left == cur {\npre.Left = child\n} else {\npre.Right = child\n}\n// \u5b50\u7ed3\u70b9\u6570\u4e3a 2\n} else {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u7ed3\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nnext := bst.getInOrderNext(cur)\ntemp := next.Val\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 next\nbst.remove(next.Val)\n// \u5c06 next \u7684\u503c\u590d\u5236\u7ed9 cur\ncur.Val = temp\n}\nreturn cur\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nfunc (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {\nif node == nil {\nreturn node\n}\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nfor node.Left != nil {\nnode = node.Left\n}\nreturn node\n}\n</code></pre> binary_search_tree.js<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nfunction remove(num) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) return null;\nlet cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) break;\npre = cur;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) return null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u7ed3\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u7ed3\u70b9 cur\nif (pre.left === cur) pre.left = child;\nelse pre.right = child;\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nlet nex = getInOrderNext(cur.right);\nlet tmp = nex.val;\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\nremove(nex.val);\n// \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur.val = tmp;\n}\nreturn cur;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nfunction getInOrderNext(root) {\nif (root === null) return root;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (root.left !== null) {\nroot = root.left;\n}\nreturn root;\n}\n</code></pre> binary_search_tree.ts<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nfunction remove(num: number): TreeNode | null {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root === null) {\nreturn null;\n}\nlet cur = root,\npre: TreeNode | null = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur !== null) {\n// \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val === num) {\nbreak;\n}\npre = cur;\nif (cur.val < num) {\ncur = cur.right as TreeNode; // \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n} else {\ncur = cur.left as TreeNode; // \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur === null) {\nreturn null;\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif (cur.left === null || cur.right === null) {\n// \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u7ed3\u70b9\nlet child = cur.left !== null ? cur.left : cur.right;\n// \u5220\u9664\u7ed3\u70b9 cur\nif (pre!.left === cur) {\npre!.left = child;\n} else {\npre!.right = child;\n}\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nlet next = getInOrderNext(cur.right);\nlet tmp = next!.val;\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\nremove(next!.val);\n// \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur.val = tmp;\n}\nreturn cur;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nfunction getInOrderNext(root: TreeNode | null): TreeNode | null {\nif (root === null) {\nreturn null;\n}\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (root.left !== null) {\nroot = root.left;\n}\nreturn root;\n}\n</code></pre> binary_search_tree.c<pre><code>[class]{binarySearchTree}-[func]{remove}\n[class]{binarySearchTree}-[func]{getInOrderNext}\n</code></pre> binary_search_tree.cs<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\nTreeNode? remove(int num)\n{\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (root == null) return null;\nTreeNode? cur = root, pre = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null)\n{\n// \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.val == num) break;\npre = cur;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.val < num) cur = cur.right;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse cur = cur.left;\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null || pre == null) return null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif (cur.left == null || cur.right == null)\n{\n// \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u7ed3\u70b9\nTreeNode? child = cur.left != null ? cur.left : cur.right;\n// \u5220\u9664\u7ed3\u70b9 cur\nif (pre.left == cur)\n{\npre.left = child;\n}\nelse\n{\npre.right = child;\n}\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2\nelse\n{\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nTreeNode? nex = getInOrderNext(cur.right);\nif (nex != null)\n{\nint tmp = nex.val;\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\nremove(nex.val);\n// \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur.val = tmp;\n}\n}\nreturn cur;\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nTreeNode? getInOrderNext(TreeNode? root)\n{\nif (root == null) return root;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (root.left != null)\n{\nroot = root.left;\n}\nreturn root;\n}\n</code></pre> binary_search_tree.swift<pre><code>/* \u5220\u9664\u7ed3\u70b9 */\n@discardableResult\nfunc remove(num: Int) -> TreeNode? {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif root == nil {\nreturn nil\n}\nvar cur = root\nvar pre: TreeNode?\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile cur != nil {\n// \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif cur!.val == num {\nbreak\n}\npre = cur\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif cur!.val < num {\ncur = cur?.right\n}\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\nelse {\ncur = cur?.left\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif cur == nil {\nreturn nil\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif cur?.left == nil || cur?.right == nil {\n// \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u7ed3\u70b9\nlet child = cur?.left != nil ? cur?.left : cur?.right\n// \u5220\u9664\u7ed3\u70b9 cur\nif pre?.left === cur {\npre?.left = child\n} else {\npre?.right = child\n}\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2\nelse {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nlet nex = getInOrderNext(root: cur?.right)\nlet tmp = nex!.val\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\nremove(num: nex!.val)\n// \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur?.val = tmp\n}\nreturn cur\n}\n/* \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09 */\nfunc getInOrderNext(root: TreeNode?) -> TreeNode? {\nvar root = root\nif root == nil {\nreturn root\n}\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile root?.left != nil {\nroot = root?.left\n}\nreturn root\n}\n</code></pre> binary_search_tree.zig<pre><code>// \u5220\u9664\u7ed3\u70b9\nfn remove(self: *Self, num: T) ?*inc.TreeNode(T) {\n// \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\nif (self.root == null) return null;\nvar cur = self.root;\nvar pre: ?*inc.TreeNode(T) = null;\n// \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u7ed3\u70b9\u540e\u8df3\u51fa\nwhile (cur != null) {\n// \u627e\u5230\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\nif (cur.?.val == num) break;\npre = cur;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\nif (cur.?.val < num) {\ncur = cur.?.right;\n// \u5f85\u5220\u9664\u7ed3\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n} else {\ncur = cur.?.left;\n}\n}\n// \u82e5\u65e0\u5f85\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\nif (cur == null) return null;\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 0 or 1\nif (cur.?.left == null or cur.?.right == null) {\n// \u5f53\u5b50\u7ed3\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u7ed3\u70b9\nvar child = if (cur.?.left != null) cur.?.left else cur.?.right;\n// \u5220\u9664\u7ed3\u70b9 cur\nif (pre.?.left == cur) {\npre.?.left = child;\n} else {\npre.?.right = child;\n}\n// \u5b50\u7ed3\u70b9\u6570\u91cf = 2\n} else {\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\nvar nex = self.getInOrderNext(cur.?.right);\nvar tmp = nex.?.val;\n// \u9012\u5f52\u5220\u9664\u7ed3\u70b9 nex\n_ = self.remove(nex.?.val);\n// \u5c06 nex \u7684\u503c\u590d\u5236\u7ed9 cur\ncur.?.val = tmp;\n}\nreturn cur;\n}\n// \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\uff08\u4ec5\u9002\u7528\u4e8e root \u6709\u5de6\u5b50\u7ed3\u70b9\u7684\u60c5\u51b5\uff09\nfn getInOrderNext(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n_ = self;\nvar node_tmp = node;\nif (node_tmp == null) return null;\n// \u5faa\u73af\u8bbf\u95ee\u5de6\u5b50\u7ed3\u70b9\uff0c\u76f4\u5230\u53f6\u7ed3\u70b9\u65f6\u4e3a\u6700\u5c0f\u7ed3\u70b9\uff0c\u8df3\u51fa\nwhile (node_tmp.?.left != null) {\nnode_tmp = node_tmp.?.left;\n}\nreturn node_tmp;\n}\n</code></pre>"},{"location":"chapter_tree/binary_search_tree/#_4","title":"\u6392\u5e8f","text":"<p>\u6211\u4eec\u77e5\u9053\uff0c\u300c\u4e2d\u5e8f\u904d\u5386\u300d\u9075\u5faa\u201c\u5de6 \\(\\rightarrow\\) \u6839 \\(\\rightarrow\\) \u53f3\u201d\u7684\u904d\u5386\u4f18\u5148\u7ea7\uff0c\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u9075\u5faa\u201c\u5de6\u5b50\u7ed3\u70b9 \\(<\\) \u6839\u7ed3\u70b9 \\(<\\) \u53f3\u5b50\u7ed3\u70b9\u201d\u7684\u5927\u5c0f\u5173\u7cfb\u3002\u56e0\u6b64\uff0c\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u8fdb\u884c\u4e2d\u5e8f\u904d\u5386\u65f6\uff0c\u603b\u662f\u4f1a\u4f18\u5148\u904d\u5386\u4e0b\u4e00\u4e2a\u6700\u5c0f\u7ed3\u70b9\uff0c\u4ece\u800c\u5f97\u51fa\u4e00\u6761\u91cd\u8981\u6027\u8d28\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217\u662f\u5347\u5e8f\u7684\u3002</p> <p>\u501f\u52a9\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\u800c\u65e0\u9700\u989d\u5916\u6392\u5e8f\uff0c\u975e\u5e38\u9ad8\u6548\u3002</p> <p></p> <p> Fig. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u4e2d\u5e8f\u904d\u5386\u5e8f\u5217 </p>"},{"location":"chapter_tree/binary_search_tree/#732","title":"7.3.2. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6548\u7387","text":"<p>\u5047\u8bbe\u7ed9\u5b9a \\(n\\) \u4e2a\u6570\u5b57\uff0c\u6700\u5e38\u7528\u7684\u5b58\u50a8\u65b9\u5f0f\u662f\u300c\u6570\u7ec4\u300d\uff0c\u90a3\u4e48\u5bf9\u4e8e\u8fd9\u4e32\u4e71\u5e8f\u7684\u6570\u5b57\uff0c\u5e38\u89c1\u64cd\u4f5c\u7684\u6548\u7387\u4e3a\uff1a</p> <ul> <li>\u67e5\u627e\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u662f\u65e0\u5e8f\u7684\uff0c\u56e0\u6b64\u9700\u8981\u904d\u5386\u6570\u7ec4\u6765\u786e\u5b9a\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b</li> <li>\u63d2\u5165\u5143\u7d20\uff1a\u53ea\u9700\u5c06\u5143\u7d20\u6dfb\u52a0\u81f3\u6570\u7ec4\u5c3e\u90e8\u5373\u53ef\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\uff1b</li> <li>\u5220\u9664\u5143\u7d20\uff1a\u5148\u67e5\u627e\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u518d\u5728\u6570\u7ec4\u4e2d\u5220\u9664\u8be5\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b</li> <li>\u83b7\u53d6\u6700\u5c0f / \u6700\u5927\u5143\u7d20\uff1a\u9700\u8981\u904d\u5386\u6570\u7ec4\u6765\u786e\u5b9a\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b</li> </ul> <p>\u4e3a\u4e86\u5f97\u5230\u5148\u9a8c\u4fe1\u606f\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u9884\u5148\u5c06\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\uff0c\u5f97\u5230\u4e00\u4e2a\u300c\u6392\u5e8f\u6570\u7ec4\u300d\uff0c\u6b64\u65f6\u64cd\u4f5c\u6548\u7387\u4e3a\uff1a</p> <ul> <li>\u67e5\u627e\u5143\u7d20\uff1a\u7531\u4e8e\u6570\u7ec4\u5df2\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\uff0c\u5e73\u5747\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff1b</li> <li>\u63d2\u5165\u5143\u7d20\uff1a\u5148\u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u518d\u63d2\u5165\u5230\u6307\u5b9a\u4f4d\u7f6e\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b</li> <li>\u5220\u9664\u5143\u7d20\uff1a\u5148\u67e5\u627e\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(\\log n)\\) \u65f6\u95f4\uff0c\u518d\u5728\u6570\u7ec4\u4e2d\u5220\u9664\u8be5\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff1b</li> <li>\u83b7\u53d6\u6700\u5c0f / \u6700\u5927\u5143\u7d20\uff1a\u6570\u7ec4\u5934\u90e8\u548c\u5c3e\u90e8\u5143\u7d20\u5373\u662f\u6700\u5c0f\u548c\u6700\u5927\u5143\u7d20\uff0c\u4f7f\u7528 \\(O(1)\\) \u65f6\u95f4\uff1b</li> </ul> <p>\u89c2\u5bdf\u53d1\u73b0\uff0c\u65e0\u5e8f\u6570\u7ec4\u548c\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u662f\u201c\u504f\u79d1\u201d\u7684\uff0c\u5373\u6709\u7684\u5feb\u6709\u7684\u6162\uff1b\u800c\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5404\u9879\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f\u5bf9\u6570\u9636\uff0c\u5728\u6570\u636e\u91cf \\(n\\) \u5f88\u5927\u65f6\u6709\u5de8\u5927\u4f18\u52bf\u3002</p> \u65e0\u5e8f\u6570\u7ec4 \u6709\u5e8f\u6570\u7ec4 \u4e8c\u53c9\u641c\u7d22\u6811 \u67e5\u627e\u6307\u5b9a\u5143\u7d20 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \u63d2\u5165\u5143\u7d20 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \u5220\u9664\u5143\u7d20 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \u83b7\u53d6\u6700\u5c0f / \u6700\u5927\u5143\u7d20 \\(O(n)\\) \\(O(1)\\) \\(O(\\log n)\\)"},{"location":"chapter_tree/binary_search_tree/#733","title":"7.3.3. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u9000\u5316","text":"<p>\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u662f\u201c\u5de6\u53f3\u5e73\u8861\u201d\u7684\uff08\u8be6\u89c1\u300c\u5e73\u8861\u4e8c\u53c9\u6811\u300d\u7ae0\u8282\uff09\uff0c\u6b64\u65f6\u53ef\u4ee5\u5728 \\(\\log n\\) \u8f6e\u5faa\u73af\u5185\u67e5\u627e\u4efb\u610f\u7ed3\u70b9\u3002</p> <p>\u5982\u679c\u6211\u4eec\u52a8\u6001\u5730\u5728\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u94fe\u8868\uff0c\u6b64\u65f6\u5404\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u9000\u5316\u4e4b \\(O(n)\\) \u3002</p> <p>Note</p> <p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5982\u4f55\u4fdd\u6301\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5e73\u8861\uff0c\u4e5f\u662f\u4e00\u4e2a\u9700\u8981\u91cd\u8981\u8003\u8651\u7684\u95ee\u9898\u3002</p> <p></p> <p> Fig. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u5e73\u8861\u4e0e\u9000\u5316 </p>"},{"location":"chapter_tree/binary_search_tree/#734","title":"7.3.4. \u00a0 \u4e8c\u53c9\u641c\u7d22\u6811\u5e38\u89c1\u5e94\u7528","text":"<ul> <li>\u7cfb\u7edf\u4e2d\u7684\u591a\u7ea7\u7d22\u5f15\uff0c\u9ad8\u6548\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u3002</li> <li>\u5404\u79cd\u641c\u7d22\u7b97\u6cd5\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u3002</li> <li>\u5b58\u50a8\u6570\u636e\u6d41\uff0c\u4fdd\u6301\u5176\u5df2\u6392\u5e8f\u3002</li> </ul>"},{"location":"chapter_tree/binary_tree/","title":"7.1. \u00a0 \u4e8c\u53c9\u6811\uff08Binary Tree\uff09","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\u7c7b\u4f3c\u4e8e\u94fe\u8868\uff0c\u4e8c\u53c9\u6811\u4e5f\u662f\u4ee5\u7ed3\u70b9\u4e3a\u5355\u4f4d\u5b58\u50a8\u7684\uff0c\u7ed3\u70b9\u5305\u542b\u300c\u503c\u300d\u548c\u4e24\u4e2a\u300c\u6307\u9488\u300d\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass TreeNode {\nint val; // \u7ed3\u70b9\u503c\nTreeNode left; // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nTreeNode right; // \u53f3\u5b50\u7ed3\u70b9\u6307\u9488\nTreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7ed3\u6784\u4f53 */\nstruct TreeNode {\nint val; // \u7ed3\u70b9\u503c\nTreeNode *left; // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nTreeNode *right; // \u53f3\u5b50\u7ed3\u70b9\u6307\u9488\nTreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n</code></pre> <pre><code>\"\"\" \u94fe\u8868\u7ed3\u70b9\u7c7b \"\"\"\nclass TreeNode:\ndef __init__(self, val=None, left=None, right=None):\nself.val = val # \u7ed3\u70b9\u503c\nself.left = left # \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nself.right = right # \u53f3\u5b50\u7ed3\u70b9\u6307\u9488\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\ntype TreeNode struct {\nVal int\nLeft *TreeNode\nRight *TreeNode\n}\n/* \u7ed3\u70b9\u521d\u59cb\u5316\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\nreturn &TreeNode{\nLeft: nil,\nRight: nil,\nVal: v,\n}\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\nfunction TreeNode(val, left, right) {\nthis.val = (val === undefined ? 0 : val); // \u7ed3\u70b9\u503c\nthis.left = (left === undefined ? null : left); // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nthis.right = (right === undefined ? null : right); // \u53f3\u5b50\u7ed3\u70b9\u6307\u9488\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\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; // \u7ed3\u70b9\u503c\nthis.left = left === undefined ? null : left; // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nthis.right = right === undefined ? null : right; // \u53f3\u5b50\u7ed3\u70b9\u6307\u9488\n}\n}\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass TreeNode {\nint val; // \u7ed3\u70b9\u503c\nTreeNode? left; // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nTreeNode? right; // \u53f3\u5b50\u7ed3\u70b9\u6307\u9488\nTreeNode(int x) { val = x; }\n}\n</code></pre> <pre><code>/* \u94fe\u8868\u7ed3\u70b9\u7c7b */\nclass TreeNode {\nvar val: Int // \u7ed3\u70b9\u503c\nvar left: TreeNode? // \u5de6\u5b50\u7ed3\u70b9\u6307\u9488\nvar right: TreeNode? // \u53f3\u5b50\u7ed3\u70b9\u6307\u9488\ninit(x: Int) {\nval = x\n}\n}\n</code></pre> <pre><code>\n</code></pre> <p>\u7ed3\u70b9\u7684\u4e24\u4e2a\u6307\u9488\u5206\u522b\u6307\u5411\u300c\u5de6\u5b50\u7ed3\u70b9 Left Child Node\u300d\u548c\u300c\u53f3\u5b50\u7ed3\u70b9 Right Child Node\u300d\uff0c\u5e76\u4e14\u79f0\u8be5\u7ed3\u70b9\u4e3a\u4e24\u4e2a\u5b50\u7ed3\u70b9\u7684\u300c\u7236\u7ed3\u70b9 Parent Node\u300d\u3002\u7ed9\u5b9a\u4e8c\u53c9\u6811\u67d0\u7ed3\u70b9\uff0c\u5c06\u5de6\u5b50\u7ed3\u70b9\u4ee5\u4e0b\u7684\u6811\u79f0\u4e3a\u8be5\u7ed3\u70b9\u7684\u300c\u5de6\u5b50\u6811 Left Subtree\u300d\uff0c\u53f3\u5b50\u6811\u540c\u7406\u3002</p> <p>\u9664\u4e86\u53f6\u7ed3\u70b9\u5916\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u90fd\u6709\u5b50\u7ed3\u70b9\u548c\u5b50\u6811\u3002\u4f8b\u5982\uff0c\u82e5\u5c06\u4e0b\u56fe\u7684\u300c\u7ed3\u70b9 2\u300d\u770b\u4f5c\u7236\u7ed3\u70b9\uff0c\u90a3\u4e48\u5176\u5de6\u5b50\u7ed3\u70b9\u548c\u53f3\u5b50\u7ed3\u70b9\u5206\u522b\u4e3a\u300c\u7ed3\u70b9 4\u300d\u548c\u300c\u7ed3\u70b9 5\u300d\uff0c\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u5206\u522b\u4e3a\u300c\u7ed3\u70b9 4 \u53ca\u5176\u4ee5\u4e0b\u7ed3\u70b9\u5f62\u6210\u7684\u6811\u300d\u548c\u300c\u7ed3\u70b9 5 \u53ca\u5176\u4ee5\u4e0b\u7ed3\u70b9\u5f62\u6210\u7684\u6811\u300d\u3002</p> <p></p> <p> Fig. \u7236\u7ed3\u70b9\u3001\u5b50\u7ed3\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\u672f\u8bed\u8f83\u591a\uff0c\u5efa\u8bae\u5c3d\u91cf\u7406\u89e3\u5e76\u8bb0\u4f4f\u3002\u540e\u7eed\u53ef\u80fd\u9057\u5fd8\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u4f7f\u7528\u65f6\u56de\u6765\u67e5\u770b\u786e\u8ba4\u3002</p> <ul> <li>\u300c\u6839\u7ed3\u70b9 Root Node\u300d\uff1a\u4e8c\u53c9\u6811\u6700\u9876\u5c42\u7684\u7ed3\u70b9\uff0c\u5176\u6ca1\u6709\u7236\u7ed3\u70b9\uff1b</li> <li>\u300c\u53f6\u7ed3\u70b9 Leaf Node\u300d\uff1a\u6ca1\u6709\u5b50\u7ed3\u70b9\u7684\u7ed3\u70b9\uff0c\u5176\u4e24\u4e2a\u6307\u9488\u90fd\u6307\u5411 \\(\\text{null}\\) \uff1b</li> <li>\u7ed3\u70b9\u6240\u5904\u300c\u5c42 Level\u300d\uff1a\u4ece\u9876\u81f3\u5e95\u4f9d\u6b21\u589e\u52a0\uff0c\u6839\u7ed3\u70b9\u6240\u5904\u5c42\u4e3a 1 \uff1b</li> <li>\u7ed3\u70b9\u300c\u5ea6 Degree\u300d\uff1a\u7ed3\u70b9\u7684\u5b50\u7ed3\u70b9\u6570\u91cf\u3002\u4e8c\u53c9\u6811\u4e2d\uff0c\u5ea6\u7684\u8303\u56f4\u662f 0, 1, 2 \uff1b</li> <li>\u300c\u8fb9 Edge\u300d\uff1a\u8fde\u63a5\u4e24\u4e2a\u7ed3\u70b9\u7684\u8fb9\uff0c\u5373\u7ed3\u70b9\u6307\u9488\uff1b</li> <li>\u4e8c\u53c9\u6811\u300c\u9ad8\u5ea6\u300d\uff1a\u4e8c\u53c9\u6811\u4e2d\u6839\u7ed3\u70b9\u5230\u6700\u8fdc\u53f6\u7ed3\u70b9\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\uff1b</li> <li>\u7ed3\u70b9\u300c\u6df1\u5ea6 Depth\u300d \uff1a\u6839\u7ed3\u70b9\u5230\u8be5\u7ed3\u70b9\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\uff1b</li> <li>\u7ed3\u70b9\u300c\u9ad8\u5ea6 Height\u300d\uff1a\u6700\u8fdc\u53f6\u7ed3\u70b9\u5230\u8be5\u7ed3\u70b9\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\uff1b</li> </ul> <p></p> <p> Fig. \u4e8c\u53c9\u6811\u7684\u5e38\u7528\u672f\u8bed </p> <p>\u9ad8\u5ea6\u4e0e\u6df1\u5ea6\u7684\u5b9a\u4e49</p> <p>\u503c\u5f97\u6ce8\u610f\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u300c\u9ad8\u5ea6\u300d\u548c\u300c\u6df1\u5ea6\u300d\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u8fb9\u7684\u6570\u91cf\u201d\uff0c\u800c\u6709\u4e9b\u9898\u76ee\u6216\u6559\u6750\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u201c\u8d70\u8fc7\u7ed3\u70b9\u7684\u6570\u91cf\u201d\uff0c\u6b64\u65f6\u9ad8\u5ea6\u6216\u6df1\u5ea6\u90fd\u9700\u8981 + 1 \u3002</p>"},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2. \u00a0 \u4e8c\u53c9\u6811\u57fa\u672c\u64cd\u4f5c","text":"<p>\u521d\u59cb\u5316\u4e8c\u53c9\u6811\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u5148\u521d\u59cb\u5316\u7ed3\u70b9\uff0c\u518d\u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_tree.java<pre><code>// \u521d\u59cb\u5316\u7ed3\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.cpp<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u7ed3\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.py<pre><code>\"\"\" \u521d\u59cb\u5316\u4e8c\u53c9\u6811 \"\"\"\n# \u521d\u59cb\u5316\u7ed3\u70b9\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.go<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u7ed3\u70b9\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n</code></pre> binary_tree.js<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u7ed3\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\u7ed3\u70b9\nlet n1 = new TreeNode(1),\nn2 = new TreeNode(2),\nn3 = new TreeNode(3),\nn4 = new TreeNode(4),\nn5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.c<pre><code>\n</code></pre> binary_tree.cs<pre><code>/* \u521d\u59cb\u5316\u4e8c\u53c9\u6811 */\n// \u521d\u59cb\u5316\u7ed3\u70b9\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n</code></pre> binary_tree.swift<pre><code>// \u521d\u59cb\u5316\u7ed3\u70b9\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u6784\u5efa\u5f15\u7528\u6307\u5411\uff08\u5373\u6307\u9488\uff09\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n</code></pre> binary_tree.zig<pre><code>\n</code></pre> <p>\u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9\u3002\u4e0e\u94fe\u8868\u7c7b\u4f3c\uff0c\u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9\u90fd\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u6307\u9488\u5b9e\u73b0\u3002</p> <p></p> <p> Fig. \u5728\u4e8c\u53c9\u6811\u4e2d\u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9 </p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_tree.java<pre><code>TreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u7ed3\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u7ed3\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.cpp<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9 */\nTreeNode* P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u7ed3\u70b9 P\nn1->left = P;\nP->left = n2;\n// \u5220\u9664\u7ed3\u70b9 P\nn1->left = n2;\n</code></pre> binary_tree.py<pre><code>\"\"\" \u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9 \"\"\"\np = TreeNode(0)\n# \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u7ed3\u70b9 P\nn1.left = p\np.left = n2\n# \u5220\u9664\u7ed3\u70b9 P\nn1.left = n2\n</code></pre> binary_tree.go<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9 */\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u7ed3\u70b9 P\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u5220\u9664\u7ed3\u70b9 P\nn1.Left = n2\n</code></pre> binary_tree.js<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9 */\nlet P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u7ed3\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u7ed3\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.ts<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9 */\nconst P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u7ed3\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u7ed3\u70b9 P\nn1.left = n2;\n</code></pre> binary_tree.c<pre><code>\n</code></pre> binary_tree.cs<pre><code>/* \u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9 */\nTreeNode P = new TreeNode(0);\n// \u5728 n1 -> n2 \u4e2d\u95f4\u63d2\u5165\u7ed3\u70b9 P\nn1.left = P;\nP.left = n2;\n// \u5220\u9664\u7ed3\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\u7ed3\u70b9 P\nn1.left = P\nP.left = n2\n// \u5220\u9664\u7ed3\u70b9 P\nn1.left = n2\n</code></pre> binary_tree.zig<pre><code>\n</code></pre> <p>Note</p> <p>\u63d2\u5165\u7ed3\u70b9\u4f1a\u6539\u53d8\u4e8c\u53c9\u6811\u7684\u539f\u6709\u903b\u8f91\u7ed3\u6784\uff0c\u5220\u9664\u7ed3\u70b9\u5f80\u5f80\u610f\u5473\u7740\u5220\u9664\u4e86\u8be5\u7ed3\u70b9\u7684\u6240\u6709\u5b50\u6811\u3002\u56e0\u6b64\uff0c\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\u6837\u624d\u80fd\u5b9e\u73b0\u6709\u610f\u4e49\u7684\u64cd\u4f5c\u3002</p>"},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3. \u00a0 \u5e38\u89c1\u4e8c\u53c9\u6811\u7c7b\u578b","text":""},{"location":"chapter_tree/binary_tree/#_1","title":"\u5b8c\u7f8e\u4e8c\u53c9\u6811","text":"<p>\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811 Perfect Binary Tree\u300d\u7684\u6240\u6709\u5c42\u7684\u7ed3\u70b9\u90fd\u88ab\u5b8c\u5168\u586b\u6ee1\u3002\u5728\u5b8c\u7f8e\u4e8c\u53c9\u6811\u4e2d\uff0c\u6240\u6709\u7ed3\u70b9\u7684\u5ea6 = 2 \uff1b\u82e5\u6811\u9ad8\u5ea6 \\(= h\\) \uff0c\u5219\u7ed3\u70b9\u603b\u6570 \\(= 2^{h+1} - 1\\) \uff0c\u5448\u6807\u51c6\u7684\u6307\u6570\u7ea7\u5173\u7cfb\uff0c\u53cd\u6620\u7740\u81ea\u7136\u754c\u4e2d\u5e38\u89c1\u7684\u7ec6\u80de\u5206\u88c2\u3002</p> <p>Tip</p> <p>\u5728\u4e2d\u6587\u793e\u533a\u4e2d\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u5e38\u88ab\u79f0\u4e3a\u300c\u6ee1\u4e8c\u53c9\u6811\u300d\uff0c\u8bf7\u6ce8\u610f\u4e0e\u5b8c\u6ee1\u4e8c\u53c9\u6811\u533a\u5206\u3002</p> <p></p> <p> Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#_2","title":"\u5b8c\u5168\u4e8c\u53c9\u6811","text":"<p>\u300c\u5b8c\u5168\u4e8c\u53c9\u6811 Complete Binary Tree\u300d\u53ea\u6709\u6700\u5e95\u5c42\u7684\u7ed3\u70b9\u672a\u88ab\u586b\u6ee1\uff0c\u4e14\u6700\u5e95\u5c42\u7ed3\u70b9\u5c3d\u91cf\u9760\u5de6\u586b\u5145\u3002</p> <p>\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002\u5982\u679c\u6309\u7167\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u987a\u5e8f\u6765\u5b58\u50a8\uff0c\u90a3\u4e48\u7a7a\u7ed3\u70b9 <code>null</code> \u4e00\u5b9a\u5168\u90e8\u51fa\u73b0\u5728\u5e8f\u5217\u7684\u5c3e\u90e8\uff0c\u56e0\u6b64\u6211\u4eec\u5c31\u53ef\u4ee5\u4e0d\u7528\u5b58\u50a8\u8fd9\u4e9b null \u4e86\u3002</p> <p></p> <p> Fig. \u5b8c\u5168\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#_3","title":"\u5b8c\u6ee1\u4e8c\u53c9\u6811","text":"<p>\u300c\u5b8c\u6ee1\u4e8c\u53c9\u6811 Full Binary Tree\u300d\u9664\u4e86\u53f6\u7ed3\u70b9\u4e4b\u5916\uff0c\u5176\u4f59\u6240\u6709\u7ed3\u70b9\u90fd\u6709\u4e24\u4e2a\u5b50\u7ed3\u70b9\u3002</p> <p></p> <p> Fig. \u5b8c\u6ee1\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#_4","title":"\u5e73\u8861\u4e8c\u53c9\u6811","text":"<p>\u300c\u5e73\u8861\u4e8c\u53c9\u6811 Balanced Binary Tree\u300d\u4e2d\u4efb\u610f\u7ed3\u70b9\u7684\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\u7684\u9ad8\u5ea6\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c \\(\\leq 1\\) \u3002</p> <p></p> <p> Fig. \u5e73\u8861\u4e8c\u53c9\u6811 </p>"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4. \u00a0 \u4e8c\u53c9\u6811\u7684\u9000\u5316","text":"<p>\u5f53\u4e8c\u53c9\u6811\u7684\u6bcf\u5c42\u7684\u7ed3\u70b9\u90fd\u88ab\u586b\u6ee1\u65f6\uff0c\u8fbe\u5230\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u300d\uff1b\u800c\u5f53\u6240\u6709\u7ed3\u70b9\u90fd\u504f\u5411\u4e00\u8fb9\u65f6\uff0c\u4e8c\u53c9\u6811\u9000\u5316\u4e3a\u300c\u94fe\u8868\u300d\u3002</p> <ul> <li>\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u4e00\u4e2a\u4e8c\u53c9\u6811\u7684\u201c\u6700\u4f73\u72b6\u6001\u201d\uff0c\u53ef\u4ee5\u5b8c\u5168\u53d1\u6325\u51fa\u4e8c\u53c9\u6811\u201c\u5206\u6cbb\u201d\u7684\u4f18\u52bf\uff1b</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)\\) \uff1b</li> </ul> <p></p> <p> Fig. \u4e8c\u53c9\u6811\u7684\u6700\u4f73\u4e0e\u6700\u4e8c\u53c9\u6811\u7684\u6700\u4f73\u548c\u6700\u5dee\u7ed3\u6784\u5dee\u60c5\u51b5 </p> <p>\u5982\u4e0b\u8868\u6240\u793a\uff0c\u5728\u6700\u4f73\u548c\u6700\u5dee\u7ed3\u6784\u4e0b\uff0c\u4e8c\u53c9\u6811\u7684\u53f6\u7ed3\u70b9\u6570\u91cf\u3001\u7ed3\u70b9\u603b\u6570\u3001\u9ad8\u5ea6\u7b49\u8fbe\u5230\u6781\u5927\u6216\u6781\u5c0f\u503c\u3002</p> \u5b8c\u7f8e\u4e8c\u53c9\u6811 \u94fe\u8868 \u7b2c \\(i\\) \u5c42\u7684\u7ed3\u70b9\u6570\u91cf \\(2^{i-1}\\) \\(1\\) \u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \u65f6\u7684\u53f6\u7ed3\u70b9\u6570\u91cf \\(2^h\\) \\(1\\) \u6811\u7684\u9ad8\u5ea6\u4e3a \\(h\\) \u65f6\u7684\u7ed3\u70b9\u603b\u6570 \\(2^{h+1} - 1\\) \\(h + 1\\) \u6811\u7684\u7ed3\u70b9\u603b\u6570\u4e3a \\(n\\) \u65f6\u7684\u9ad8\u5ea6 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree/#715","title":"7.1.5. \u00a0 \u4e8c\u53c9\u6811\u8868\u793a\u65b9\u5f0f *","text":"<p>\u6211\u4eec\u4e00\u822c\u4f7f\u7528\u4e8c\u53c9\u6811\u7684\u300c\u94fe\u8868\u8868\u793a\u300d\uff0c\u5373\u5b58\u50a8\u5355\u4f4d\u4e3a\u7ed3\u70b9 <code>TreeNode</code> \uff0c\u7ed3\u70b9\u4e4b\u95f4\u901a\u8fc7\u6307\u9488\uff08\u5f15\u7528\uff09\u76f8\u8fde\u63a5\u3002\u672c\u6587\u524d\u8ff0\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u4e8c\u53c9\u6811\u5728\u94fe\u8868\u8868\u793a\u4e0b\u7684\u5404\u9879\u57fa\u672c\u64cd\u4f5c\u3002</p> <p>\u90a3\u80fd\u5426\u53ef\u4ee5\u7528\u300c\u6570\u7ec4\u8868\u793a\u300d\u4e8c\u53c9\u6811\u5462\uff1f\u7b54\u6848\u662f\u80af\u5b9a\u7684\u3002\u5148\u6765\u5206\u6790\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u300c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u300d\uff0c\u5c06\u7ed3\u70b9\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u7f16\u53f7\uff08\u4ece 0 \u5f00\u59cb\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u63a8\u5bfc\u5f97\u51fa\u7236\u7ed3\u70b9\u7d22\u5f15\u4e0e\u5b50\u7ed3\u70b9\u7d22\u5f15\u4e4b\u95f4\u7684\u300c\u6620\u5c04\u516c\u5f0f\u300d\uff1a\u8bbe\u7ed3\u70b9\u7684\u7d22\u5f15\u4e3a \\(i\\) \uff0c\u5219\u8be5\u7ed3\u70b9\u7684\u5de6\u5b50\u7ed3\u70b9\u7d22\u5f15\u4e3a \\(2i + 1\\) \u3001\u53f3\u5b50\u7ed3\u70b9\u7d22\u5f15\u4e3a \\(2i + 2\\) \u3002</p> <p>\u672c\u8d28\u4e0a\uff0c\u6620\u5c04\u516c\u5f0f\u7684\u4f5c\u7528\u5c31\u662f\u94fe\u8868\u4e2d\u7684\u6307\u9488\u3002\u5bf9\u4e8e\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u4e2d\u7684\u4efb\u610f\u7ed3\u70b9\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u4f7f\u7528\u6620\u5c04\u516c\u5f0f\u6765\u8bbf\u95ee\u5b50\u7ed3\u70b9\u3002\u56e0\u6b64\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\uff08\u5373\u6570\u7ec4\uff09\u6765\u8868\u793a\u5b8c\u7f8e\u4e8c\u53c9\u6811\u3002</p> <p></p> <p> Fig. \u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a </p> <p>\u7136\u800c\uff0c\u5b8c\u7f8e\u4e8c\u53c9\u6811\u53ea\u662f\u4e2a\u4f8b\uff0c\u4e8c\u53c9\u6811\u4e2d\u95f4\u5c42\u5f80\u5f80\u5b58\u5728\u8bb8\u591a\u7a7a\u7ed3\u70b9\uff08\u5373 <code>null</code> \uff09\uff0c\u800c\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u5e76\u4e0d\u5305\u542b\u8fd9\u4e9b\u7a7a\u7ed3\u70b9\uff0c\u5e76\u4e14\u6211\u4eec\u65e0\u6cd5\u5355\u51ed\u5e8f\u5217\u6765\u731c\u6d4b\u7a7a\u7ed3\u70b9\u7684\u6570\u91cf\u548c\u5206\u5e03\u4f4d\u7f6e\uff0c\u5373\u7406\u8bba\u4e0a\u5b58\u5728\u8bb8\u591a\u79cd\u4e8c\u53c9\u6811\u90fd\u7b26\u5408\u8be5\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u3002\u663e\u7136\uff0c\u8fd9\u79cd\u60c5\u51b5\u65e0\u6cd5\u4f7f\u7528\u6570\u7ec4\u6765\u5b58\u50a8\u4e8c\u53c9\u6811\u3002</p> <p></p> <p> Fig. \u7ed9\u5b9a\u6570\u7ec4\u5bf9\u5e94\u591a\u79cd\u4e8c\u53c9\u6811\u53ef\u80fd\u6027 </p> <p>\u4e3a\u4e86\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8003\u8651\u6309\u7167\u5b8c\u7f8e\u4e8c\u53c9\u6811\u7684\u5f62\u5f0f\u6765\u8868\u793a\u6240\u6709\u4e8c\u53c9\u6811\uff0c\u5373\u5728\u5e8f\u5217\u4e2d\u4f7f\u7528\u7279\u6b8a\u7b26\u53f7\u6765\u663e\u5f0f\u5730\u8868\u793a\u201c\u7a7a\u4f4d\u201d\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u8fd9\u6837\u5904\u7406\u540e\uff0c\u5e8f\u5217\uff08\u6570\u7ec4\uff09\u5c31\u53ef\u4ee5\u552f\u4e00\u8868\u793a\u4e8c\u53c9\u6811\u4e86\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig <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// \u4e3a\u4e86\u7b26\u5408\u6570\u636e\u7c7b\u578b\u4e3a int \uff0c\u4f7f\u7528 int \u6700\u5927\u503c\u6807\u8bb0\u7a7a\u4f4d\n// \u8be5\u65b9\u6cd5\u7684\u4f7f\u7528\u524d\u63d0\u662f\u6ca1\u6709\u7ed3\u70b9\u7684\u503c = INT_MAX\nvector<int> tree = { 1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15 };\n</code></pre> <pre><code>\"\"\" \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a \"\"\"\n# \u76f4\u63a5\u4f7f\u7528 None \u6765\u8868\u793a\u7a7a\u4f4d\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u76f4\u63a5\u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u76f4\u63a5\u4f7f\u7528 null \u6765\u8868\u793a\u7a7a\u4f4d\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n</code></pre> <pre><code>\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 null \u6765\u6807\u8bb0\u7a7a\u4f4d\nint?[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n</code></pre> <pre><code>/* \u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a */\n// \u4f7f\u7528 Int? \u53ef\u7a7a\u7c7b\u578b \uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 nil \u6765\u6807\u8bb0\u7a7a\u4f4d\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n</code></pre> <pre><code>\n</code></pre> <p></p> <p> Fig. \u4efb\u610f\u7c7b\u578b\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a </p> <p>\u56de\u987e\u300c\u5b8c\u5168\u4e8c\u53c9\u6811\u300d\u7684\u5b9a\u4e49\uff0c\u5176\u53ea\u6709\u6700\u5e95\u5c42\u6709\u7a7a\u7ed3\u70b9\uff0c\u5e76\u4e14\u6700\u5e95\u5c42\u7684\u7ed3\u70b9\u5c3d\u91cf\u9760\u5de6\uff0c\u56e0\u800c\u6240\u6709\u7a7a\u7ed3\u70b9\u90fd\u4e00\u5b9a\u51fa\u73b0\u5728\u5c42\u5e8f\u904d\u5386\u5e8f\u5217\u7684\u672b\u5c3e\u3002\u56e0\u4e3a\u6211\u4eec\u5148\u9a8c\u5730\u786e\u5b9a\u4e86\u7a7a\u4f4d\u7684\u4f4d\u7f6e\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u6570\u7ec4\u8868\u793a\u5b8c\u5168\u4e8c\u53c9\u6811\u65f6\uff0c\u53ef\u4ee5\u7701\u7565\u5b58\u50a8\u201c\u7a7a\u4f4d\u201d\u3002\u56e0\u6b64\uff0c\u5b8c\u5168\u4e8c\u53c9\u6811\u975e\u5e38\u9002\u5408\u4f7f\u7528\u6570\u7ec4\u6765\u8868\u793a\u3002</p> <p></p> <p> Fig. \u5b8c\u5168\u4e8c\u53c9\u6811\u7684\u6570\u7ec4\u8868\u793a </p> <p>\u6570\u7ec4\u8868\u793a\u6709\u4e24\u4e2a\u4f18\u70b9\uff1a \u4e00\u662f\u4e0d\u9700\u8981\u5b58\u50a8\u6307\u9488\uff0c\u8282\u7701\u7a7a\u95f4\uff1b\u4e8c\u662f\u53ef\u4ee5\u968f\u673a\u8bbf\u95ee\u7ed3\u70b9\u3002\u7136\u800c\uff0c\u5f53\u4e8c\u53c9\u6811\u4e2d\u7684\u201c\u7a7a\u4f4d\u201d\u5f88\u591a\u65f6\uff0c\u6570\u7ec4\u4e2d\u53ea\u5305\u542b\u5f88\u5c11\u7ed3\u70b9\u7684\u6570\u636e\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u5f88\u4f4e\u3002</p>"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2. \u00a0 \u4e8c\u53c9\u6811\u904d\u5386","text":"<p>\u4ece\u7269\u7406\u7ed3\u6784\u89d2\u5ea6\u770b\uff0c\u6811\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u56e0\u6b64\u904d\u5386\u65b9\u5f0f\u4e5f\u662f\u901a\u8fc7\u6307\u9488\uff08\u5373\u5f15\u7528\uff09\u9010\u4e2a\u904d\u5386\u7ed3\u70b9\u3002\u540c\u65f6\uff0c\u6811\u8fd8\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u5bfc\u81f4\u904d\u5386\u6811\u6bd4\u904d\u5386\u94fe\u8868\u66f4\u52a0\u590d\u6742\uff0c\u9700\u8981\u4f7f\u7528\u641c\u7d22\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002</p> <p>\u5e38\u89c1\u7684\u4e8c\u53c9\u6811\u904d\u5386\u65b9\u5f0f\u6709\u5c42\u5e8f\u904d\u5386\u3001\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3002</p>"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1. \u00a0 \u5c42\u5e8f\u904d\u5386","text":"<p>\u300c\u5c42\u5e8f\u904d\u5386 Level-Order Traversal\u300d\u4ece\u9876\u81f3\u5e95\u3001\u4e00\u5c42\u4e00\u5c42\u5730\u904d\u5386\u4e8c\u53c9\u6811\uff0c\u5e76\u5728\u6bcf\u5c42\u4e2d\u6309\u7167\u4ece\u5de6\u5230\u53f3\u7684\u987a\u5e8f\u8bbf\u95ee\u7ed3\u70b9\u3002</p> <p>\u5c42\u5e8f\u904d\u5386\u672c\u8d28\u4e0a\u662f\u300c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 Breadth-First Traversal\u300d\uff0c\u5176\u4f53\u73b0\u7740\u4e00\u79cd\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u201d\u7684\u5c42\u8fdb\u904d\u5386\u65b9\u5f0f\u3002</p> <p></p> <p> Fig. \u4e8c\u53c9\u6811\u7684\u5c42\u5e8f\u904d\u5386 </p>"},{"location":"chapter_tree/binary_tree_traversal/#_1","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"<p>\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u4e00\u822c\u501f\u52a9\u300c\u961f\u5217\u300d\u6765\u5b9e\u73b0\u3002\u961f\u5217\u7684\u89c4\u5219\u662f\u201c\u5148\u8fdb\u5148\u51fa\u201d\uff0c\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u7684\u89c4\u5219\u662f \u201d\u4e00\u5c42\u5c42\u5e73\u63a8\u201c \uff0c\u4e24\u8005\u80cc\u540e\u7684\u601d\u60f3\u662f\u4e00\u81f4\u7684\u3002</p> JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_tree_bfs.java<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nList<Integer> levelOrder(TreeNode root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nQueue<TreeNode> queue = new LinkedList<>() {{ add(root); }};\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<Integer> list = new ArrayList<>();\nwhile (!queue.isEmpty()) {\nTreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\nlist.add(node.val); // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif (node.left != null)\nqueue.offer(node.left); // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\nif (node.right != null)\nqueue.offer(node.right); // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.cpp<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder(TreeNode* root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nqueue<TreeNode*> queue;\nqueue.push(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvector<int> vec;\nwhile (!queue.empty()) {\nTreeNode* node = queue.front();\nqueue.pop(); // \u961f\u5217\u51fa\u961f\nvec.push_back(node->val); // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif (node->left != nullptr)\nqueue.push(node->left); // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\nif (node->right != nullptr)\nqueue.push(node->right); // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n}\nreturn vec;\n}\n</code></pre> binary_tree_bfs.py<pre><code>\"\"\" \u5c42\u5e8f\u904d\u5386 \"\"\"\ndef level_order(root: Optional[TreeNode]):\n# \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nqueue = collections.deque()\nqueue.append(root)\n# \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nres = []\nwhile queue:\nnode = queue.popleft() # \u961f\u5217\u51fa\u961f\nres.append(node.val) # \u4fdd\u5b58\u7ed3\u70b9\u503c\nif node.left is not None:\nqueue.append(node.left) # \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\nif node.right is not None:\nqueue.append(node.right) # \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\nreturn res\n</code></pre> binary_tree_bfs.go<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []int {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nqueue := list.New()\nqueue.PushBack(root)\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nnums := make([]int, 0)\nfor queue.Len() > 0 {\n// poll\nnode := queue.Remove(queue.Front()).(*TreeNode)\n// \u4fdd\u5b58\u7ed3\u70b9\u503c\nnums = append(nums, node.Val)\nif node.Left != nil {\n// \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\nqueue.PushBack(node.Left)\n}\nif node.Right != nil {\n// \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\nqueue.PushBack(node.Right)\n}\n}\nreturn nums\n}\n</code></pre> binary_tree_bfs.js<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nlet queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nlet list = [];\nwhile (queue.length) {\nlet node = queue.shift(); // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif (node.left)\nqueue.push(node.left); // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\nif (node.right)\nqueue.push(node.right); // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.ts<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nconst queue = [root];\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nconst list: number[] = [];\nwhile (queue.length) {\nlet node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\nlist.push(node.val); // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif (node.left) {\nqueue.push(node.left); // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\n}\nif (node.right) {\nqueue.push(node.right); // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n}\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.c<pre><code>[class]{}-[func]{levelOrder}\n</code></pre> binary_tree_bfs.cs<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder(TreeNode root)\n{\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nQueue<TreeNode> queue = new();\nqueue.Enqueue(root);\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nList<int> list = new();\nwhile (queue.Count != 0)\n{\nTreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\nlist.Add(node.val); // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif (node.left != null)\nqueue.Enqueue(node.left); // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\nif (node.right != null)\nqueue.Enqueue(node.right); // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n}\nreturn list;\n}\n</code></pre> binary_tree_bfs.swift<pre><code>/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nvar queue: [TreeNode] = [root]\n// \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list: [Int] = []\nwhile !queue.isEmpty {\nlet node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\nlist.append(node.val) // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif let left = node.left {\nqueue.append(left) // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\n}\nif let right = node.right {\nqueue.append(right) // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n}\n}\nreturn list\n}\n</code></pre> binary_tree_bfs.zig<pre><code>// \u5c42\u5e8f\u904d\u5386\nfn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {\n// \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u7ed3\u70b9\nconst L = std.TailQueue(*inc.TreeNode(T));\nvar queue = L{};\nvar root_node = try mem_allocator.create(L.Node);\nroot_node.data = root;\nqueue.append(root_node); // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\nvar list = std.ArrayList(T).init(std.heap.page_allocator);\nwhile (queue.len > 0) {\nvar queue_node = queue.popFirst().?; // \u961f\u5217\u51fa\u961f\nvar node = queue_node.data;\ntry list.append(node.val); // \u4fdd\u5b58\u7ed3\u70b9\u503c\nif (node.left != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.left.?;\nqueue.append(tmp_node); // \u5de6\u5b50\u7ed3\u70b9\u5165\u961f\n}\nif (node.right != null) {\nvar tmp_node = try mem_allocator.create(L.Node);\ntmp_node.data = node.right.?;\nqueue.append(tmp_node); // \u53f3\u5b50\u7ed3\u70b9\u5165\u961f\n} }\nreturn list;\n}\n</code></pre>"},{"location":"chapter_tree/binary_tree_traversal/#_2","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u7ed3\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7ed3\u70b9\u6570\u91cf\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5f53\u4e3a\u6ee1\u4e8c\u53c9\u6811\u65f6\u8fbe\u5230\u6700\u5dee\u60c5\u51b5\uff0c\u904d\u5386\u5230\u6700\u5e95\u5c42\u524d\uff0c\u961f\u5217\u4e2d\u6700\u591a\u540c\u65f6\u5b58\u5728 \\(\\frac{n + 1}{2}\\) \u4e2a\u7ed3\u70b9\uff0c\u4f7f\u7528 \\(O(n)\\) \u7a7a\u95f4\u3002</p>"},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2. \u00a0 \u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386","text":"<p>\u76f8\u5bf9\u5730\uff0c\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\u7686\u5c5e\u4e8e\u300c\u6df1\u5ea6\u4f18\u5148\u904d\u5386 Depth-First Traversal\u300d\uff0c\u5176\u4f53\u73b0\u7740\u4e00\u79cd\u201c\u5148\u8d70\u5230\u5c3d\u5934\uff0c\u518d\u56de\u5934\u7ee7\u7eed\u201d\u7684\u56de\u6eaf\u904d\u5386\u65b9\u5f0f\u3002</p> <p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5de6\u4fa7\u662f\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u7684\u7684\u793a\u610f\u56fe\uff0c\u53f3\u4e0a\u65b9\u662f\u5bf9\u5e94\u7684\u9012\u5f52\u5b9e\u73b0\u4ee3\u7801\u3002\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u5c31\u50cf\u662f\u7ed5\u7740\u6574\u4e2a\u4e8c\u53c9\u6811\u7684\u5916\u56f4\u201c\u8d70\u201d\u4e00\u5708\uff0c\u8d70\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5728\u6bcf\u4e2a\u7ed3\u70b9\u90fd\u4f1a\u9047\u5230\u4e09\u4e2a\u4f4d\u7f6e\uff0c\u5206\u522b\u5bf9\u5e94\u524d\u5e8f\u904d\u5386\u3001\u4e2d\u5e8f\u904d\u5386\u3001\u540e\u5e8f\u904d\u5386\u3002</p> <p></p> <p> Fig. \u4e8c\u53c9\u641c\u7d22\u6811\u7684\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386 </p> \u4f4d\u7f6e \u542b\u4e49 \u6b64\u5904\u8bbf\u95ee\u7ed3\u70b9\u65f6\u5bf9\u5e94 \u6a59\u8272\u5706\u5708\u5904 \u521a\u8fdb\u5165\u6b64\u7ed3\u70b9\uff0c\u5373\u5c06\u8bbf\u95ee\u8be5\u7ed3\u70b9\u7684\u5de6\u5b50\u6811 \u524d\u5e8f\u904d\u5386 Pre-Order Traversal \u84dd\u8272\u5706\u5708\u5904 \u5df2\u8bbf\u95ee\u5b8c\u5de6\u5b50\u6811\uff0c\u5373\u5c06\u8bbf\u95ee\u53f3\u5b50\u6811 \u4e2d\u5e8f\u904d\u5386 In-Order Traversal \u7d2b\u8272\u5706\u5708\u5904 \u5df2\u8bbf\u95ee\u5b8c\u5de6\u5b50\u6811\u548c\u53f3\u5b50\u6811\uff0c\u5373\u5c06\u8fd4\u56de \u540e\u5e8f\u904d\u5386 Post-Order Traversal"},{"location":"chapter_tree/binary_tree_traversal/#_3","title":"\u7b97\u6cd5\u5b9e\u73b0","text":"JavaC++PythonGoJavaScriptTypeScriptCC#SwiftZig binary_tree_dfs.java<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.add(root.val);\n}\n</code></pre> binary_tree_dfs.cpp<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode* root) {\nif (root == nullptr) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nvec.push_back(root->val);\npreOrder(root->left);\npreOrder(root->right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode* root) {\nif (root == nullptr) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root->left);\nvec.push_back(root->val);\ninOrder(root->right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode* root) {\nif (root == nullptr) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npostOrder(root->left);\npostOrder(root->right);\nvec.push_back(root->val);\n}\n</code></pre> binary_tree_dfs.py<pre><code>\"\"\" \u524d\u5e8f\u904d\u5386 \"\"\"\ndef pre_order(root: Optional[TreeNode]):\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nres.append(root.val)\npre_order(root=root.left)\npre_order(root=root.right)\n\"\"\" \u4e2d\u5e8f\u904d\u5386 \"\"\"\ndef in_order(root: Optional[TreeNode]):\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\nin_order(root=root.left)\nres.append(root.val)\nin_order(root=root.right)\n\"\"\" \u540e\u5e8f\u904d\u5386 \"\"\"\ndef post_order(root: Optional[TreeNode]):\nif root is None:\nreturn\n# \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npost_order(root=root.left)\npost_order(root=root.right)\nres.append(root.val)\n</code></pre> binary_tree_dfs.go<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nnums = append(nums, node.Val)\npreOrder(node.Left)\npreOrder(node.Right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ninOrder(node.Left)\nnums = append(nums, node.Val)\ninOrder(node.Right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\nif node == nil {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npostOrder(node.Left)\npostOrder(node.Right)\nnums = append(nums, node.Val)\n}\n</code></pre> binary_tree_dfs.js<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\nif (root === null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\n</code></pre> binary_tree_dfs.ts<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.push(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.push(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\nif (root === null) {\nreturn;\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.push(root.val);\n}\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.cs<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? root)\n{\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.Add(root.val);\npreOrder(root.left);\npreOrder(root.right);\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? root)\n{\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root.left);\nlist.Add(root.val);\ninOrder(root.right);\n}\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? root)\n{\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npostOrder(root.left);\npostOrder(root.right);\nlist.Add(root.val);\n}\n</code></pre> binary_tree_dfs.swift<pre><code>/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\nlist.append(root.val)\npreOrder(root: root.left)\npreOrder(root: root.right)\n}\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ninOrder(root: root.left)\nlist.append(root.val)\ninOrder(root: root.right)\n}\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\nguard let root = root else {\nreturn\n}\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\npostOrder(root: root.left)\npostOrder(root: root.right)\nlist.append(root.val)\n}\n</code></pre> binary_tree_dfs.zig<pre><code>// \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u7ed3\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\ntry list.append(root.?.val);\ntry preOrder(T, root.?.left);\ntry preOrder(T, root.?.right);\n}\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u7ed3\u70b9 -> \u53f3\u5b50\u6811\ntry inOrder(T, root.?.left);\ntry list.append(root.?.val);\ntry inOrder(T, root.?.right);\n}\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\nif (root == null) return;\n// \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u7ed3\u70b9\ntry postOrder(T, root.?.left);\ntry postOrder(T, root.?.right);\ntry list.append(root.?.val);\n}\n</code></pre> <p>Note</p> <p>\u4f7f\u7528\u5faa\u73af\u4e00\u6837\u53ef\u4ee5\u5b9e\u73b0\u524d\u3001\u4e2d\u3001\u540e\u5e8f\u904d\u5386\uff0c\u4f46\u4ee3\u7801\u76f8\u5bf9\u7e41\u7410\uff0c\u6709\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u81ea\u884c\u5b9e\u73b0\u3002</p>"},{"location":"chapter_tree/binary_tree_traversal/#_4","title":"\u590d\u6742\u5ea6\u5206\u6790","text":"<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a\u6240\u6709\u7ed3\u70b9\u88ab\u8bbf\u95ee\u4e00\u6b21\uff0c\u4f7f\u7528 \\(O(n)\\) \u65f6\u95f4\uff0c\u5176\u4e2d \\(n\\) \u4e3a\u7ed3\u70b9\u6570\u91cf\u3002</p> <p>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a\u5f53\u6811\u9000\u5316\u4e3a\u94fe\u8868\u65f6\u8fbe\u5230\u6700\u5dee\u60c5\u51b5\uff0c\u9012\u5f52\u6df1\u5ea6\u8fbe\u5230 \\(n\\) \uff0c\u7cfb\u7edf\u4f7f\u7528 \\(O(n)\\) \u6808\u5e27\u7a7a\u95f4\u3002</p>"},{"location":"chapter_tree/summary/","title":"7.5. \u00a0 \u5c0f\u7ed3","text":""},{"location":"chapter_tree/summary/#_1","title":"\u4e8c\u53c9\u6811","text":"<ul> <li>\u4e8c\u53c9\u6811\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4ee3\u8868\u7740\u201c\u4e00\u5206\u4e3a\u4e8c\u201d\u7684\u5206\u6cbb\u903b\u8f91\u3002\u4e8c\u53c9\u6811\u7684\u7ed3\u70b9\u5305\u542b\u300c\u503c\u300d\u548c\u4e24\u4e2a\u300c\u6307\u9488\u300d\uff0c\u5206\u522b\u6307\u5411\u5de6\u5b50\u7ed3\u70b9\u548c\u53f3\u5b50\u7ed3\u70b9\u3002</li> <li>\u9009\u5b9a\u4e8c\u53c9\u6811\u4e2d\u67d0\u7ed3\u70b9\uff0c\u5c06\u5176\u5de6\uff08\u53f3\uff09\u5b50\u7ed3\u70b9\u4ee5\u4e0b\u5f62\u6210\u7684\u6811\u79f0\u4e3a\u5de6\uff08\u53f3\uff09\u5b50\u6811\u3002</li> <li>\u4e8c\u53c9\u6811\u7684\u672f\u8bed\u8f83\u591a\uff0c\u5305\u62ec\u6839\u7ed3\u70b9\u3001\u53f6\u7ed3\u70b9\u3001\u5c42\u3001\u5ea6\u3001\u8fb9\u3001\u9ad8\u5ea6\u3001\u6df1\u5ea6\u7b49\u3002</li> <li>\u4e8c\u53c9\u6811\u7684\u521d\u59cb\u5316\u3001\u7ed3\u70b9\u63d2\u5165\u3001\u7ed3\u70b9\u5220\u9664\u64cd\u4f5c\u4e0e\u94fe\u8868\u7684\u64cd\u4f5c\u65b9\u6cd5\u7c7b\u4f3c\u3002</li> <li>\u5e38\u89c1\u7684\u4e8c\u53c9\u6811\u7c7b\u578b\u5305\u62ec\u5b8c\u7f8e\u4e8c\u53c9\u6811\u3001\u5b8c\u5168\u4e8c\u53c9\u6811\u3001\u5b8c\u6ee1\u4e8c\u53c9\u6811\u3001\u5e73\u8861\u4e8c\u53c9\u6811\u3002\u5b8c\u7f8e\u4e8c\u53c9\u6811\u662f\u7406\u60f3\u72b6\u6001\uff0c\u94fe\u8868\u5219\u662f\u9000\u5316\u540e\u7684\u6700\u5dee\u72b6\u6001\u3002</li> <li>\u4e8c\u53c9\u6811\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u8868\u793a\uff0c\u5177\u4f53\u505a\u6cd5\u662f\u5c06\u7ed3\u70b9\u503c\u548c\u7a7a\u4f4d\u6309\u7167\u5c42\u5e8f\u904d\u5386\u7684\u987a\u5e8f\u6392\u5217\uff0c\u5e76\u57fa\u4e8e\u7236\u7ed3\u70b9\u548c\u5b50\u7ed3\u70b9\u4e4b\u95f4\u7684\u7d22\u5f15\u6620\u5c04\u516c\u5f0f\u5b9e\u73b0\u6307\u9488\u3002</li> </ul>"},{"location":"chapter_tree/summary/#_2","title":"\u4e8c\u53c9\u6811\u904d\u5386","text":"<ul> <li>\u4e8c\u53c9\u6811\u5c42\u5e8f\u904d\u5386\u662f\u4e00\u79cd\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f53\u73b0\u7740\u201c\u4e00\u5708\u4e00\u5708\u5411\u5916\u201d\u7684\u5c42\u8fdb\u5f0f\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u501f\u52a9\u961f\u5217\u6765\u5b9e\u73b0\u3002</li> <li>\u524d\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\u904d\u5386\u662f\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f53\u73b0\u7740\u201c\u8d70\u5230\u5934\u3001\u518d\u56de\u5934\u7ee7\u7eed\u201d\u7684\u56de\u6eaf\u904d\u5386\u65b9\u5f0f\uff0c\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u3002</li> </ul>"},{"location":"chapter_tree/summary/#_3","title":"\u4e8c\u53c9\u641c\u7d22\u6811","text":"<ul> <li>\u4e8c\u53c9\u641c\u7d22\u6811\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u5143\u7d20\u67e5\u627e\u6570\u636e\u7ed3\u6784\uff0c\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u7686\u4e3a \\(O(\\log n)\\) \u3002\u4e8c\u53c9\u641c\u7d22\u6811\u9000\u5316\u4e3a\u94fe\u8868\u540e\uff0c\u5404\u9879\u65f6\u95f4\u590d\u6742\u5ea6\u52a3\u5316\u81f3 \\(O(n)\\) \uff0c\u56e0\u6b64\u5982\u4f55\u907f\u514d\u9000\u5316\u662f\u975e\u5e38\u91cd\u8981\u7684\u8bfe\u9898\u3002</li> <li>AVL \u6811\u53c8\u79f0\u5e73\u8861\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u5176\u901a\u8fc7\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u5f97\u5728\u4e0d\u65ad\u63d2\u5165\u4e0e\u5220\u9664\u7ed3\u70b9\u540e\uff0c\u4ecd\u7136\u53ef\u4ee5\u4fdd\u6301\u4e8c\u53c9\u6811\u7684\u5e73\u8861\uff08\u4e0d\u9000\u5316\uff09\u3002</li> <li>AVL \u6811\u7684\u65cb\u8f6c\u64cd\u4f5c\u5206\u4e3a\u53f3\u65cb\u3001\u5de6\u65cb\u3001\u5148\u53f3\u65cb\u540e\u5de6\u65cb\u3001\u5148\u5de6\u65cb\u540e\u53f3\u65cb\u3002\u5728\u63d2\u5165\u6216\u5220\u9664\u7ed3\u70b9\u540e\uff0cAVL \u6811\u4f1a\u4ece\u5e95\u81f3\u9876\u5730\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u6811\u6062\u590d\u5e73\u8861\u3002</li> </ul>"}]} |