{"id":1168,"date":"2023-05-04T13:37:49","date_gmt":"2023-05-04T05:37:49","guid":{"rendered":"https:\/\/white-night.club\/?p=1168"},"modified":"2023-11-09T11:07:55","modified_gmt":"2023-11-09T03:07:55","slug":"ad10","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/05\/04\/ad10\/","title":{"rendered":"35.\u00a0\u641c\u7d22\u63d2\u5165\u4f4d\u7f6e"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6392\u5e8f\u6570\u7ec4\u548c\u4e00\u4e2a\u76ee\u6807\u503c\uff0c\u5728\u6570\u7ec4\u4e2d\u627e\u5230\u76ee\u6807\u503c\uff0c\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u3002\u5982\u679c\u76ee\u6807\u503c\u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\uff0c\u8fd4\u56de\u5b83\u5c06\u4f1a\u88ab\u6309\u987a\u5e8f\u63d2\u5165\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u8bf7\u5fc5\u987b\u4f7f\u7528\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a&nbsp;<code>O(log n)<\/code>&nbsp;\u7684\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> nums = [1,3,5,6], target = 5\n<strong>\u8f93\u51fa:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> nums = [1,3,5,6], target = 2\n<strong>\u8f93\u51fa:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> nums = [1,3,5,6], target = 7\n<strong>\u8f93\u51fa:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a:<\/strong><\/p>\n\n\n\n<ul>\n<li><code>1 &lt;= nums.length &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>-10<sup>4<\/sup> &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>nums<\/code>&nbsp;\u4e3a&nbsp;<strong>\u65e0\u91cd\u590d\u5143\u7d20&nbsp;<\/strong>\u7684&nbsp;<strong>\u5347\u5e8f&nbsp;<\/strong>\u6392\u5217\u6570\u7ec4<\/li>\n\n\n\n<li><code>-10<sup>4<\/sup> &lt;= target &lt;= 10<sup>4<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u521d\u59cb\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int searchInsert(vector&lt;int&gt;&amp; nums, int target) {\n        \n    }\n};<\/code><\/pre>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u4e8c\u5206\u67e5\u627e\uff08\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(logn)\uff09<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u770b\u5230O(logn)\u5c31\u5728\u60f3\u5e94\u8be5\u7528\u4e8c\u5206\u67e5\u627e\u5199\u4e86\u3002\u4e0d\u8fc7\u8fd9\u9898\u9700\u8981\u8003\u8651\u6ca1\u627e\u5230\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>vector\u4e0b\u6807\u4ece0\u5f00\u59cb\u3002\u6240\u4ee5right=nums.size()\u8fd8\u9700\u8981-1\u3002<\/p>\n\n\n\n<p>left\u548cright\u4e0d\u76f8\u7b49\u662f\u4e3a\u4e86\u5904\u7406\u8fb9\u754c\u3002\u6bd4\u5982vector\u4e2d\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u65f6\uff0c\u5982\u679c\u6b64\u65f6\u6ca1\u627e\u5230\uff0c\u90a3\u4e48left\u53ef\u80fd\u7b49\u4e8e1\uff0c\u6216right\u53ef\u80fd\u7b49\u4e8e-1.\u5982\u679c\u6b64\u65f6\u518d\u53bb\u548cnums[left]\u5c31\u4f1a\u6570\u7ec4\u8d8a\u754c\u3002<\/p>\n\n\n\n<p>\u90a3\u5c31\u628a\u8fd9\u79cd\u60c5\u51b5\u62c6\u51fa\u6765\u7528if\u8bed\u53e5\u8868\u793a\u5373\u53ef\u3002\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u5b8c\u6574\u4ee3\u7801<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int searchInsert(vector&lt;int&gt;&amp; nums, int target) {\n        int left=0,right=nums.size()-1;\n        while(left&lt;right)\n        {\n            int mid=(left+right)\/2;\n            if(nums&#91;mid]==target)\n                return mid;\n            else if(nums&#91;mid]&gt;target)\n                right=mid-1;\n            else if(nums&#91;mid]&lt;target)\n                left=mid+1;\n        }\n        if(target&gt;nums&#91;left])\n            return left+1;\n        else\n            return left;\n    }\n};<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>LeetCode\u9898\u76ee\uff1a\u4e8c\u5206\u67e5\u627e<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1168"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=1168"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1168\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=1168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=1168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}