{"id":377,"date":"2026-01-14T00:51:33","date_gmt":"2026-01-13T16:51:33","guid":{"rendered":"https:\/\/jdoxk3.cn\/?p=377"},"modified":"2026-01-16T00:25:28","modified_gmt":"2026-01-15T16:25:28","slug":"%e4%bb%a3%e7%a0%81%e9%9a%8f%e6%83%b3%e5%bd%95%e7%ae%97%e6%b3%95%e8%ae%ad%e7%bb%83%e8%90%a5%e7%ac%ac%e4%b8%80%e5%a4%a9-704-%e4%ba%8c%e5%88%86%e6%9f%a5%e6%89%be-27-%e7%a7%bb%e9%99%a4%e5%85%83","status":"publish","type":"post","link":"https:\/\/jdoxk3.cn\/index.php\/2026\/01\/14\/%e4%bb%a3%e7%a0%81%e9%9a%8f%e6%83%b3%e5%bd%95%e7%ae%97%e6%b3%95%e8%ae%ad%e7%bb%83%e8%90%a5%e7%ac%ac%e4%b8%80%e5%a4%a9-704-%e4%ba%8c%e5%88%86%e6%9f%a5%e6%89%be-27-%e7%a7%bb%e9%99%a4%e5%85%83\/","title":{"rendered":"\u4ee3\u7801\u968f\u60f3\u5f55\u7b97\u6cd5\u8bad\u7ec3\u8425\u7b2c\u4e00\u5929 | 704.\u4e8c\u5206\u67e5\u627e, 27.\u79fb\u9664\u5143\u7d20, 977.\u6709\u5e8f\u6570\u7ec4\u7684\u5e73\u65b9"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u524d\u8a00<\/h1>\n\n\n\n<p>\u4eca\u5929\u662f\u7b97\u6cd5\u8bad\u7ec3\u8425\u5f00\u8425\u7b2c\u4e00\u5929\uff0c\u5e0c\u671b\u80fd\u5728\u540e\u9762\u7684\u65e5\u5b50\u91cc\u5b8c\u6574\u5237\u5b8c\u4e00\u6b21\u9898\u5355\u3002\u7136\u800c\uff0c\u5237\u5b8c\u4e00\u6b21\u9898\u5355\u7684flag\u4ece 2024 \u4e0a\u534a\u5e74\u5c31\u5df2\u7acb\u4e0b\uff0c\u6bcf\u6b21\u90fd\u56e0\u4e3a\u5404\u79cd\u5404\u6837\u7684\u539f\u56e0\u5bfc\u81f4\u6ca1\u80fd\u5b8c\u6210\u8ba1\u5212\uff0c\u4e0d\u8bf4\u884c\u767e\u91cc\u8005\u534a\u4e5d\u5341\uff0c\u6bcf\u6b21\u91cd\u65b0\u62fe\u8d77\u6765\u90fd\u4ece\u5934\u5f00\u59cb\uff0c\u78e8\u4e86\u4e00\u5e74\u534a\u4e86\u8fdb\u5ea6\u4f9d\u65e7\u5c11\u5f97\u53ef\u601c\u3002<\/p>\n\n\n\n<p>\u8fd9\u6b21\uff0c\u6211\u60f3\u8d70\u5230\u6700\u540e\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">704.\u4e8c\u5206\u67e5\u627e<\/h1>\n\n\n\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/binary-search\/description\/\">704.\u4e8c\u5206\u67e5\u627e<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u601d\u8def<\/h2>\n\n\n\n<p>\u9996\u5148\u8bf4\u4e0b\u601d\u8def\uff0c\u8fd9\u9053\u9898\u76ee\u5df2\u7ecf\u4ea4\u624b\u8fc7\u5f88\u591a\u6b21\u4e86\uff0c\u601d\u8def\u4e3b\u8981\u662f\u7528\u66b4\u529b\u548c\u4e8c\u5206\u6cd5<\/p>\n\n\n\n<p>\u66b4\u529b\u7684\u601d\u8def\u6bd4\u8f83\u7b80\u5355\uff0c\u904d\u5386\u8be5\u6570\u7ec4\u5982\u679c\u627e\u5230\u76ee\u6807\u503c\u5c31\u8fd4\u56de\u4e0b\u6807\u8fd9\u91cc\u4e0d\u8fc7\u591a\u8d58\u8ff0\u3002<\/p>\n\n\n\n<p>\u4e8c\u5206\u6cd5\u662f\u8fd9\u9053\u9898\u7684\u4e3b\u9898\uff0c\u4e8c\u5206\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u4f4e\uff0c\u4e8c\u5206\u6cd5\u7684\u5199\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1a\u4e00\u79cd\u662f\u5de6\u95ed\u53f3\u95ed\u533a\u95f4\u53e6\u4e00\u79cd\u662f\u5de6\u95ed\u53f3\u5f00\u533a\u95f4\uff0c\u8fd9\u9053\u9898\u6211\u4e2a\u4eba\u6bd4\u8f83\u503e\u5411\u524d\u8005\uff0c\u8fd9\u4e24\u8005\u7684\u533a\u522b\u4e3b\u8981\u4f53\u73b0\u5728\u5199\u5faa\u73af\u6761\u4ef6\u548c\u5224\u65admiddle\u66f4\u65b0\u903b\u8f91\u65f6\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f7f\u7528\u5de6\u95ed\u53f3\u95ed\uff0c\u5728\u5224\u65ad\u5faa\u73af\u6761\u4ef6\u65f6\u53ef\u4ee5\u5b58\u5728left == right \u7684\u60c5\u51b5\uff0c\u800c\u540e\u8005\u4e0d\u884c\uff1b\u5728\u5224\u65admiddle \u66f4\u65b0\u903b\u8f91\u65f6\uff0c\u540e\u8005\u5c06right\u503c\u76f4\u63a5\u66f4\u65b0\u4e3amiddle\uff0c\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u67e5\u8be2\u533a\u95f4\u5728[left, middle)\u4e2d\uff0c\u4e0d\u4f1a\u6bd4\u8f83nums[middle] \uff0c\u6240\u4ee5\u6ca1\u5fc5\u8981\u51cf 1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u66b4\u529b\u5199\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><br> class Solution:<br>\u00a0 \u00a0 \u00a0def search(self, nums: List&#91;int], target: int) -> int:<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0for i in range(0,len(nums)):<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0if nums&#91;i] == target:<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0return i<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0return -1<br><br><\/code><\/pre>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u5206\u6cd5\u5199\u6cd5\uff08\u4ec5\u8bb0\u5f55\u5de6\u95ed\u53f3\u95ed\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class Solution:<br>&nbsp; &nbsp; &nbsp;def search(self, nums: List&#91;int], target: int) -&gt; int:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = len(nums) - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while left &lt;= right:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mid = (left + right) \/\/ 2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;mid] &lt; target:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = mid + 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;mid] &gt; target:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = mid - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;mid] == target:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return mid<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return -1<\/code><\/pre>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(log n)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9644\u52a0\u9898\uff5c35.\u641c\u7d22\u63d2\u5165\u4f4d\u7f6e<\/h2>\n\n\n\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/search-insert-position\/description\/\">35.\u641c\u7d22\u63d2\u5165\u4f4d\u7f6e<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u5206\u6cd5\u5199\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class Solution:<br>&nbsp; &nbsp; &nbsp;def searchInsert(self, nums: List&#91;int], target: int) -&gt; int:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = len(nums) - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while left &lt;= right:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mid = (left + right) \/\/ 2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;mid] &lt; target:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = mid + 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;elif nums&#91;mid] &gt; target:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = mid - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return mid<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return right + 1<\/code><\/pre>\n\n\n\n<p>\u601d\u8def\u4e0e 704 \u9898\u5927\u81f4\u4e00\u81f4\uff0c\u53ea\u662f\u65b0\u589e\u4e86\u5982\u679c\u6700\u7ec8\u6ca1\u627e\u5230\u76ee\u6807\u503c\u7684\u63d2\u5165\u4f4d\u7f6e\u5224\u65ad\u903b\u8f91<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(log n)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9644\u52a0\u9898\uff5c34.\u5728\u6392\u5e8f\u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e<\/h2>\n\n\n\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/find-first-and-last-position-of-element-in-sorted-array\/\">34.\u5728\u6392\u5e8f\u6570\u7ec4\u4e2d\u67e5\u627e\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u548c\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53cc\u6307\u9488\u5199\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class Solution:<br>&nbsp; &nbsp; &nbsp;def searchRange(self, nums: List&#91;int], target: int) -&gt; List&#91;int]:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = 0 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = len(nums) - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lb = -2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rb = -2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while left &lt; len(nums):<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;left] == target:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lb = left<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left += 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while right &gt;= 0:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;right] == target:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rb = right<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right -= 1<br>&nbsp;\u200b<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if lb != -2 and rb != -2:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &#91;lb, rb]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if lb != -2 and rb == -2:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &#91;lb, lb]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if lb == -2 and rb != -2:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &#91;rb, rb]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &#91;-1,-1]<\/code><\/pre>\n\n\n\n<p>\u8fd9\u9053\u9898\u548c\u4e4b\u524d\u7a0d\u6709\u4e0d\u540c\uff0c\u6211\u7684\u7b54\u9898\u601d\u8def\u662f\u5728\u4e24\u7aef\u5b9a\u4e49\u6307\u9488\uff0c\u4e24\u6bb5\u6307\u9488\u5206\u522b\u5411\u4e2d\u95f4\u5185\u6536\uff0c\u5982\u679c\u627e\u5230 target \u503c\u5c31\u8fdb\u884c\u8bb0\u5f55\u5230\u8fb9\u754c\u503c\u4f5c\u4e3a\u7ed3\u679c\uff0c\u540c\u65f6\u7ed3\u675f\u5faa\u73af\uff0c\u6700\u7ec8\u5c06\u4e24\u4e2a\u8fb9\u754c\u8f93\u51fa\u3002\u4e8c\u5206\u6cd5\u7684\u603b\u4f53\u601d\u8def\u4e0e\u4e4b\u76f8\u540c\uff0c\u53ea\u662f\u5728\u5bfb\u627e\u8fb9\u754c\u503c\u7684\u8fc7\u7a0b\u4e2d\u4ea7\u751f\u533a\u522b\uff0c\u4f46\u7531\u4e8e\u8fd9\u9053\u9898\u6570\u7ec4\u91cc\u7684\u5143\u7d20\u662f\u987a\u5e8f\u6392\u5217\u7684\u6240\u4ee5\u7528\u53cc\u6307\u9488\u5185\u6536\u6211\u4e2a\u4eba\u89c9\u5f97\u76f8\u5bf9\u597d\u7406\u89e3\u4e00\u4e9b\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">27.\u79fb\u9664\u5143\u7d20<\/h1>\n\n\n\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/remove-element\/\">27. \u79fb\u9664\u5143\u7d20<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u601d\u8def<\/h2>\n\n\n\n<p>\u8fd9\u9053\u9898\u7684\u5927\u4f53\u601d\u8def\u662f\u66b4\u529b\u548c\u53cc\u6307\u9488\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u89e3\u6cd5\u601d\u8def\u4e0a\u5b58\u5728\u5171\u540c\u70b9\uff0c\u66b4\u529b\u505a\u6cd5\u662f\u904d\u5386\u6574\u4e2a\u6570\u7ec4\uff0c\u5982\u679c\u627e\u5230\u76ee\u6807\u503c\uff0c\u5219\u5c06\u540e\u9762\u6240\u6709\u5143\u7d20\u524d\u79fb\u4e00\u4f4d\uff0c\u540c\u65f6\u5c06\u6574\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u51cf\u4e00\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u5bfc\u81f4\u6570\u7ec4\u5927\u5c0f\u5728\u5143\u7d20\u5220\u9664\u540e\u5f02\u5e38\uff1b\u53cc\u6307\u9488\u6cd5\u662f\u5b9a\u4e49\u5feb\u6162\u6307\u9488\uff0c\u5982\u679c\u5feb\u6307\u9488\u6240\u6307\u975e\u76ee\u6807\u503c\uff0c\u5219\u5c06\u6162\u6307\u9488\u5904\u6570\u503c\u66f4\u65b0\u4e3a\u5feb\u6307\u9488\u5904\u6570\u503c\uff0c\u540c\u65f6\u6162\u6307\u9488\u540e\u79fb\u4e00\u4f4d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u66b4\u529b\u5199\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class Solution:<br>&nbsp; &nbsp; &nbsp;def removeElement(self, nums: List&#91;int], val: int) -&gt; int:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size = len(nums)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while i &lt; size:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;i] == val:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j = i + 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while j &lt; size:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nums&#91;j - 1] = nums&#91;j]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j += 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i -= 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;size -= 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i += 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return size<\/code><\/pre>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n ^ 2)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53cc\u6307\u9488\u5199\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class Solution:<br>&nbsp; &nbsp; &nbsp;def removeElement(self, nums: List&#91;int], val: int) -&gt; int:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;slow = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fast = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while fast &lt; len(nums):<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;fast] != val:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nums&#91;slow] = nums&#91;fast]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;slow += 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fast += 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return slow<\/code><\/pre>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">977.\u6709\u5e8f\u6570\u7ec4\u7684\u5e73\u65b9<\/h1>\n\n\n\n<p>\u9898\u76ee\u94fe\u63a5\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/squares-of-a-sorted-array\/\">977. \u6709\u5e8f\u6570\u7ec4\u7684\u5e73\u65b9<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u601d\u8def<\/h2>\n\n\n\n<p>\u8fd9\u9053\u9898\u7684\u601d\u8def\u4e3b\u8981\u662f\u66b4\u529b\u548c\u53cc\u6307\u9488\u6cd5\uff0c\u66b4\u529b\u6bd4\u8f83\u65e0\u804a\u6240\u4ee5\u8fd9\u91cc\u4e0d\u7ee7\u7eed\u5c55\u793a\uff0c\u6211\u4eec\u4e3b\u8981\u4f7f\u7528\u7684\u662f\u53cc\u6307\u9488\u6cd5\uff0c\u8fd9\u9053\u9898\u91cc\u6700\u5173\u952e\u7684\u8003\u91cf\u70b9\u662f\u6570\u7ec4\u5185\u539f\u672c\u7684\u8d1f\u6570\u5728\u5e73\u65b9\u540e\u4f1a\u653e\u5728\u4ec0\u4e48\u4f4d\u7f6e\uff0c\u56e0\u6b64\u6211\u4eec\u4e3b\u8981\u5de5\u4f5c\u662f\u5c06\u8d1f\u6570\u5e73\u65b9\u4e0e\u6b63\u6570\u5e73\u65b9\u6bd4\u8f83\u903b\u8f91\u7406\u6e05\uff0c\u90a3\u4e48\u5728\u8fd9\u9053\u9898\u91cc\u6211\u4eec\u53c8\u65b0\u5efa\u4e86\u4e00\u4e2a\u957f\u5ea6\u76f8\u7b49\u7684\u6570\u7ec4\u6765\u5b58\u653e\u7ed3\u679c\uff0c\u800c\u6211\u4eec\u7684\u7ed3\u679c\u5b58\u653e\u65b9\u5f0f\u4e5f\u662f\u4ece\u6700\u5927\u5f80\u6700\u5c0f\u5f00\u59cb\u653e\uff0c\u5728\u6bcf\u6b21\u5b58\u653e\u524d\u6bd4\u8f83\u5de6\u6307\u9488\u4e0e\u53f3\u6307\u9488\u6240\u6307\u503c\u5e73\u65b9\u5927\u5c0f\uff0c\u6839\u636e\u7ed3\u679c\u5b58\u653e\u6570\u503c\u5e76\u8c03\u6574\u6307\u9488\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53cc\u6307\u9488\u5199\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;class Solution:<br>&nbsp; &nbsp; &nbsp;def sortedSquares(self, nums: List&#91;int]) -&gt; List&#91;int]:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = 0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = len(nums) - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k = len(nums) - 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = &#91;float('inf')] * len(nums)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while left &lt;= right:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if nums&#91;left] * nums&#91;left] &lt; nums&#91;right] * nums&#91;right]:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result&#91;k] = nums&#91;right] * nums&#91;right]<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right -=1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result&#91;k] = nums&#91;left] * nums&#91;left] <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left += 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k -= 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return result<\/code><\/pre>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n)<\/p>\n\n\n\n<p>\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3aO(n)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u603b\u7ed3<\/h1>\n\n\n\n<p>\u5728\u7b97\u6cd5\u8bad\u7ec3\u8425\u7b2c\u4e00\u5929\u7684\u9898\u76ee\u91cc\uff0c\u6211\u4eec\u4e3b\u8981\u5b66\u4e60\u548c\u7ec3\u4e60\u4e86\u66b4\u529b\u3001\u4e8c\u5206\u6cd5\u548c\u53cc\u6307\u9488\u6cd5\u4e09\u79cd\u89e3\u9898\u601d\u8def\uff0c\u5176\u4e2d\u4e8c\u5206\u6cd5\u9700\u8981\u6ce8\u610f\u5de6\u95ed\u53f3\u95ed\u548c\u5de6\u95ed\u53f3\u5f00\u4e24\u79cd\u65b9\u5f0f\u7684\u4e66\u5199\u60c5\u51b5\u6709\u6240\u533a\u522b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u4eca\u5929\u662f\u7b97\u6cd5\u8bad\u7ec3\u8425\u5f00\u8425\u7b2c\u4e00\u5929\uff0c\u5e0c\u671b\u80fd\u5728\u540e\u9762\u7684\u65e5\u5b50\u91cc\u5b8c\u6574\u5237\u5b8c\u4e00\u6b21\u9898\u5355\u3002\u7136\u800c\uff0c\u5237\u5b8c\u4e00\u6b21\u9898\u5355\u7684flag\u4ece 202 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[13,15,14],"class_list":["post-377","post","type-post","status-publish","format-standard","hentry","category-python","tag-python","tag-15","tag-14"],"_links":{"self":[{"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/posts\/377","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/comments?post=377"}],"version-history":[{"count":4,"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":382,"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/posts\/377\/revisions\/382"}],"wp:attachment":[{"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/media?parent=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdoxk3.cn\/index.php\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}