{"id":2102,"date":"2022-01-16T13:30:57","date_gmt":"2022-01-16T05:30:57","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2102"},"modified":"2022-01-16T13:31:10","modified_gmt":"2022-01-16T05:31:10","slug":"leetcodeday35","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/16\/leetcodeday35\/","title":{"rendered":"leetcodeday35 -\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-code\"><code><strong>\u8f93\u5165:<\/strong> nums = &#91;1,3,5,6], target = 5\n<strong>\u8f93\u51fa:<\/strong> 2<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> nums = &#91;1,3,5,6], target = 2\n<strong>\u8f93\u51fa:<\/strong> 1<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> nums = &#91;1,3,5,6], target = 7\n<strong>\u8f93\u51fa:<\/strong> 4<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> nums = &#91;1,3,5,6], target = 0\n<strong>\u8f93\u51fa:<\/strong> 0<\/code><\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\u8f93\u5165:<\/strong> nums = &#91;1], target = 0\n<strong>\u8f93\u51fa:<\/strong> 0<\/code><\/pre>\n\n\n\n<p><strong>\u63d0\u793a:<\/strong><\/p>\n\n\n\n<ul><li><code>1 &lt;= nums.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>-10<sup>4<\/sup> &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>nums<\/code>&nbsp;\u4e3a<strong>\u65e0\u91cd\u590d\u5143\u7d20<\/strong>\u7684<strong>\u5347\u5e8f<\/strong>\u6392\u5217\u6570\u7ec4<\/li><li><code>-10<sup>4<\/sup> &lt;= target &lt;= 10<sup>4<\/sup><\/code><\/li><\/ul>\n\n\n\n<p>\u601d\u8def \uff1a \u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u00a0<code>O(log n)<\/code>\u00a0\u7684\u7b97\u6cd5 \u9996\u5148\u60f3\u5230\u7684\u662f\u4e8c\u5206\u67e5\u627e\uff0c\u9012\u5f52\uff0c\u6bcf\u6b21\u67e5\u627emid\u503c\uff0c\u5982\u679c\u5927\u4e8emid\uff0c\u5728\u53f3\u8fb9\u5bfb\u627e\uff0c\u5426\u5219\u5728\u5de6\u8fb9\u5bfb\u627e\u3002<\/p>\n\n\n\n<p>\u4e8c\u5206\u641c\u7d22\u662f\u4e00\u79cd\u5728\u6709\u5e8f\u6570\u7ec4\u4e2d\u67e5\u627e\u67d0\u4e00\u7279\u5b9a\u5143\u7d20\u7684\u641c\u7d22\u7b97\u6cd5\u3002\u641c\u7d22\u8fc7\u7a0b\u4ece\u6570\u7ec4\u7684\u4e2d\u95f4\u5143\u7d20\u5f00\u59cb\uff0c\u5982\u679c\u4e2d\u95f4\u5143\u7d20\u6b63\u597d\u662f\u8981\u67e5\u627e\u7684\u5143\u7d20\uff0c\u5219\u641c\u7d22\u8fc7\u7a0b\u7ed3\u675f\uff1b\u5982\u679c\u67d0\u4e00\u7279\u5b9a\u5143\u7d20\u5927\u4e8e\u6216\u8005\u5c0f\u4e8e\u4e2d\u95f4\u5143\u7d20\uff0c\u5219\u5728\u6570\u7ec4\u5927\u4e8e\u6216\u5c0f\u4e8e\u4e2d\u95f4\u5143\u7d20\u7684\u90a3\u4e00\u534a\u4e2d\u67e5\u627e\uff0c\u800c\u4e14\u8ddf\u5f00\u59cb\u4e00\u6837\u4ece\u4e2d\u95f4\u5143\u7d20\u5f00\u59cb\u6bd4\u8f83\u3002\u5982\u679c\u5728\u67d0\u4e00\u6b65\u9aa4\u6570\u7ec4\u4e3a\u7a7a\uff0c\u5219\u4ee3\u8868\u627e\u4e0d\u5230\u3002\u8fd9\u79cd\u641c\u7d22\u7b97\u6cd5\u6bcf\u4e00\u6b21\u6bd4\u8f83\u90fd\u4f7f\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u4e00\u534a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\n#\u601d\u8def\uff1a\u4e8c\u5206\u67e5\u627e\r\nclass Solution:\r\n    def searchInsert(self, nums: List&#91;int], target: int) -> int:\r\n        def binarySearch (arr, l, r, x): \r\n  \r\n            # \u57fa\u672c\u5224\u65ad\r\n            if r >= l: \r\n  \r\n                mid = int(l + (r - l)\/2)\r\n\r\n  \r\n            # \u5143\u7d20\u6574\u597d\u7684\u4e2d\u95f4\u4f4d\u7f6e\r\n                if arr&#91;mid] == x: \r\n                    return mid \r\n          \r\n        # \u5143\u7d20\u5c0f\u4e8e\u4e2d\u95f4\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u53ea\u9700\u8981\u518d\u6bd4\u8f83\u5de6\u8fb9\u7684\u5143\u7d20\r\n                elif arr&#91;mid] > x: \r\n                    return binarySearch(arr, l, mid-1, x) \r\n  \r\n        # \u5143\u7d20\u5927\u4e8e\u4e2d\u95f4\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u53ea\u9700\u8981\u518d\u6bd4\u8f83\u53f3\u8fb9\u7684\u5143\u7d20\r\n                else: \r\n                    return binarySearch(arr, mid+1, r, x) \r\n  \r\n            else: \r\n        # \u4e0d\u5b58\u5728\r\n                return r+1\r\n        return binarySearch(nums,0, len(nums)-1,target )\r\n# @lc code=end<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"795\" height=\"192\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-233.png\" alt=\"\" class=\"wp-image-2103\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-233.png 795w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-233-300x72.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-233-768x185.png 768w\" sizes=\"(max-width: 795px) 100vw, 795px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<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 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/16\/leetcodeday35\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday35 -\u641c\u7d22\u63d2\u5165\u4f4d\u7f6e<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2102"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2102"}],"version-history":[{"count":3,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2102\/revisions"}],"predecessor-version":[{"id":2106,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2102\/revisions\/2106"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}