{"id":71,"date":"2026-03-29T11:50:18","date_gmt":"2026-03-29T03:50:18","guid":{"rendered":"https:\/\/geocommunity.vip\/?p=71"},"modified":"2026-03-29T15:16:15","modified_gmt":"2026-03-29T07:16:15","slug":"%e4%ba%8c%e5%88%86%e6%9f%a5%e6%89%be%e8%93%9d%e6%a1%a5%e6%9d%af%e5%a4%87%e8%b5%9b","status":"publish","type":"post","link":"https:\/\/geocommunity.vip\/index.php\/2026\/03\/29\/%e4%ba%8c%e5%88%86%e6%9f%a5%e6%89%be%e8%93%9d%e6%a1%a5%e6%9d%af%e5%a4%87%e8%b5%9b\/","title":{"rendered":"\u4e8c\u5206\u67e5\u627e(\u84dd\u6865\u676f\u5907\u8d5b)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1\u3001\u5bfb\u627e\u5cf0\u503c<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5cf0\u503c\u5143\u7d20\u662f\u6307\u5176\u503c<strong>\u4e25\u683c\u5927\u4e8e<\/strong>\u5de6\u53f3\u76f8\u90bb\u503c\u7684\u5143\u7d20\u3002\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 <code>nums<\/code>\uff0c\u627e\u5230\u5cf0\u503c\u5143\u7d20\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u3002\u6570\u7ec4\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u5cf0\u503c\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8fd4\u56de<strong>\u4efb\u4f55\u4e00\u4e2a\u5cf0\u503c<\/strong>\u6240\u5728\u4f4d\u7f6e\u5373\u53ef\u3002\u4f60\u53ef\u4ee5\u5047\u8bbe <code>nums[-1] = nums[n] = -\u221e<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u5fc5\u987b\u5b9e\u73b0\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a <strong>O(log n)<\/strong> \u7684\u7b97\u6cd5\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9898\u76ee\u5df2\u77e5\u6700\u6700\u5de6\u8fb9\u548c\u6700\u53f3\u8fb9\u90fd\u662f\u6df1\u6e0a\uff0c\u6211\u4eec\u6bd4\u8f83nums[mid]\u548cnums[mid+1]\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679cnums[mid] > nums[mid+1]\uff0c\u5219\u5f53\u524d\u4e3a\u4e0b\u5761\uff0c\u90a3\u4e48mid\u6216\u8005mid\u5de6\u8fb9\u4e00\u5b9a\u6709\u4e00\u5ea7\u5c71\u5cf0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679cnums[mid] &lt; nums[mid+1]\uff0c\u90a3\u4e48\u5f53\u524d\u4e3a\u4e0a\u5761\uff0c\u90a3\u4e48mid\u53f3\u8fb9\u4e00\u5b9a\u6709\u4e00\u5ea7\u5c71\u5cf0\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679cnums[mid] = nums[mid+1]\uff0c\u90a3\u4e48\u5f53\u524d\u4e3a\u5e73\u5730\uff0c\u53c8&#8221;\u5cf0\u503c<strong>\u4e25\u683c\u5927\u4e8e<\/strong>\u5de6\u53f3\u76f8\u90bb\u503c&#8221;\uff0c\u6240\u4ee5\u8fd9\u4e00\u5b9a\u4e0d\u662f\u5c71\u5cf0\uff0c\u90a3\u4e48\u5176\u5de6\u8fb9\u6216\u53f3\u8fb9\u4e00\u5b9a\u6709\u4e00\u5ea7\u5c71\u5cf0\u3002\uff08\u8fd9\u79cd\u60c5\u51b5\u5148\u4e0d\u8ba8\u8bba\uff0c\u6bd4\u8f83\u590d\u6742\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u9053\u9898\u76ee\u7684\u5355\u8c03\u6027\u6bd4\u8f83\u62bd\u8c61\uff0c\u8ba4\u771f\u601d\u8003\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(3)\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int findPeakElement(int* nums, int numsSize) {\n\n\u00a0 \u00a0 int left = 0;\n\n\u00a0 \u00a0 int right = numsSize - 1;\n\n\u00a0 \u00a0 while (left &lt; right) {\n\n\u00a0 \u00a0 \u00a0 \u00a0 int mid = left + (right - left) \/ 2;\n\n\u00a0 \u00a0 \u00a0 \u00a0 if (nums&#91;mid] > nums&#91;mid+1]) right = mid;\n\n\u00a0 \u00a0 \u00a0 \u00a0 if (nums&#91;mid] &lt; nums&#91;mid+1]) left = mid + 1;\n\n\u00a0 \u00a0 }\n\n\u00a0 \u00a0 return left;\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2\u3001\u6709\u5e8f\u77e9\u9635\u4e2d\u7b2c K \u5c0f\u7684\u5143\u7d20<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\uff081\uff09\u9898\u76ee\u63cf\u8ff0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ed9\u4f60\u4e00\u4e2a <code>n x n<\/code> \u77e9\u9635 <code>matrix<\/code>\uff0c\u5176\u4e2d\u6bcf\u884c\u548c\u6bcf\u5217\u5143\u7d20\u5747\u6309\u5347\u5e8f\u6392\u5e8f\uff0c\u627e\u5230\u77e9\u9635\u4e2d\u7b2c <code>k<\/code> \u5c0f\u7684\u5143\u7d20\u3002\u8bf7\u6ce8\u610f\uff0c\u5b83\u662f<strong>\u6392\u5e8f\u540e<\/strong>\u7684\u7b2c <code>k<\/code> \u5c0f\u5143\u7d20\uff0c\u800c\u4e0d\u662f\u7b2c <code>k<\/code> \u4e2a<strong>\u4e0d\u540c<\/strong>\u7684\u5143\u7d20\u3002\u4f60\u5fc5\u987b\u627e\u5230\u4e00\u4e2a\u5185\u5b58\u590d\u6742\u5ea6\u4f18\u4e8e O(n\u00b2) \u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff082\uff09\u89e3\u9898\u601d\u8def<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u65ad\u679a\u4e3e <code>mid<\/code>\uff0c\u5de6\u8fb9\u754c\u662f <code>matrix[0][0]<\/code>\uff0c\u53f3\u8fb9\u754c\u662f <code>matrix[n-1][n-1]<\/code><\/li>\n\n\n\n<li>\u5f53\u6570\u7ec4\u4e2d <code>&lt;= mid<\/code> \u7684\u4e2a\u6570 <code>>= k<\/code>\uff1a<code>high = mid<\/code><\/li>\n\n\n\n<li>\u5f53\u4e2a\u6570 <code>&lt; k<\/code>\uff1a<code>low = mid + 1<\/code><\/li>\n\n\n\n<li>\u8303\u56f4\u4e0d\u65ad\u5408\u7406\u7f29\u5c0f\uff0c\u53ea\u6709\u5f53 <code>low == high<\/code> \u5c31\u662f\u7b54\u6848<\/li>\n\n\n\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(n * log(high &#8211; low))<\/li>\n\n\n\n<li>\u5355\u8c03\u6027\uff1a\u6570\u8d8a\u5c0f\uff0c\u6bd4\u4ed6\u5c0f\u7684\u8d8a\u5c11<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\uff083\uff09\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8f85\u52a9\u51fd\u6570\uff1a\u7edf\u8ba1\u77e9\u9635\u4e2d &lt;= mid \u7684\u5143\u7d20\u4e2a\u6570\nint check(int** matrix, int n, int mid) {\n    int count = 0;\n    int r = n - 1;\n    int c = 0;\n\n    while (r >= 0 &amp;&amp; c &lt; n) {\n        if (matrix&#91;r]&#91;c] &lt;= mid) {\n            count += (r + 1);\n            c++;\n        } else {\n            r--;\n        }\n    }\n    return count;\n}\n\nint kthSmallest(int** matrix, int matrixSize, int* matrixColSize, int k) {\n    int n = matrixSize;\n    int low = matrix&#91;0]&#91;0];\n    int high = matrix&#91;n - 1]&#91;n - 1];\n\n    while (low &lt; high) {\n        int mid = low + (high - low) \/ 2;\n\n        if (check(matrix, n, mid) >= k) {\n            high = mid; \/\/ \u53ef\u80fdmid\u5c31\u662f\u7b54\u6848\n        } else {\n            low = mid + 1; \/\/ mid\u4e00\u5b9a\u4e0d\u662f\u7b54\u6848\n        }\n    }\n\n    return low;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5bfb\u627e\u5cf0\u503c \uff081\uff09\u9898\u76ee\u63cf\u8ff0 \u5cf0\u503c\u5143\u7d20\u662f\u6307\u5176\u503c\u4e25\u683c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":2,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":73,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/posts\/71\/revisions\/73"}],"wp:attachment":[{"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geocommunity.vip\/index.php\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}