{"id":2403,"date":"2022-03-21T23:59:26","date_gmt":"2022-03-21T15:59:26","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2403"},"modified":"2022-03-24T10:51:28","modified_gmt":"2022-03-24T02:51:28","slug":"%e4%ba%8c%e5%88%86%e6%9f%a5%e6%89%be","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2403","title":{"rendered":"\u7b97\u6cd5\u5165\u95e8\u7b2c\u4e00\u5929"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u4e8c\u5206\u67e5\u627e<\/h1>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/study-plan\/algorithms\/?progress=7ob9vsg\">\u300c\u7b97\u6cd5\u300d &#8211; \u5b66\u4e60\u8ba1\u5212 &#8211; \u529b\u6263\uff08LeetCode\uff09\u5168\u7403\u6781\u5ba2\u631a\u7231\u7684\u6280\u672f\u6210\u957f\u5e73\u53f0 (leetcode-cn.com)<\/a><\/p>\n\n\n\n<p>\u8ddf\u7740\u8fd9\u4e2aleecode\u7684\u8ba1\u5212\u590d\u4e60\u4e0b\u8fd9\u4e9b\u7b97\u6cd5<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><a href=\"https:\/\/leetcode-cn.com\/problems\/binary-search\/\">704. \u4e8c\u5206\u67e5\u627e<\/a><\/h4>\n\n\n\n<p>\u96be\u5ea6 \u7b80\u5355<\/p>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a&nbsp;<code>n<\/code>&nbsp;\u4e2a\u5143\u7d20\u6709\u5e8f\u7684\uff08\u5347\u5e8f\uff09\u6574\u578b\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\u548c\u4e00\u4e2a\u76ee\u6807\u503c&nbsp;<code>target<\/code>&nbsp;&nbsp;\uff0c\u5199\u4e00\u4e2a\u51fd\u6570\u641c\u7d22&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u7684&nbsp;<code>target<\/code>\uff0c\u5982\u679c\u76ee\u6807\u503c\u5b58\u5728\u8fd4\u56de\u4e0b\u6807\uff0c\u5426\u5219\u8fd4\u56de&nbsp;<code>-1<\/code>\u3002<\/p>\n\n\n\n<p><br><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> <code>nums<\/code> = [-1,0,3,5,9,12], <code>target<\/code> = 9\n<strong>\u8f93\u51fa:<\/strong> 4\n<strong>\u89e3\u91ca:<\/strong> 9 \u51fa\u73b0\u5728 <code>nums<\/code> \u4e2d\u5e76\u4e14\u4e0b\u6807\u4e3a 4\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> <code>nums<\/code> = [-1,0,3,5,9,12], <code>target<\/code> = 2\n<strong>\u8f93\u51fa:<\/strong> -1\n<strong>\u89e3\u91ca:<\/strong> 2 \u4e0d\u5b58\u5728 <code>nums<\/code> \u4e2d\u56e0\u6b64\u8fd4\u56de -1\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ol><li>\u4f60\u53ef\u4ee5\u5047\u8bbe&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u7684\u6240\u6709\u5143\u7d20\u662f\u4e0d\u91cd\u590d\u7684\u3002<\/li><li><code>n<\/code>&nbsp;\u5c06\u5728&nbsp;<code>[1, 10000]<\/code>\u4e4b\u95f4\u3002<\/li><li><code>nums<\/code>&nbsp;\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u5c06\u5728&nbsp;<code>[-9999, 9999]<\/code>\u4e4b\u95f4\u3002<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Question704 {\n\tpublic static int search(int[] nums, int target) {\n\t\tint low = 0; int high = nums.length - 1;\n\t\twhile (low&lt;=high){\n\t\t\tint mid = (high + low) \/ 2;\n\t\t\tif(nums[mid] == target) {\n\t\t\t\treturn mid;\n\t\t\t} else if(nums[mid] &lt; target) {\n\t\t\t\tlow = mid + 1;\n\t\t\t} else if(nums[mid] &gt; target) {\n\t\t\t\thigh = mid - 1;\n\t\t\t}\n\t\t}\n\t\treturn -1;\n\t}\n\n\n\tpublic static void main(String args[]){\n\t\tSystem.out.println(search(new int[]{-1,0,3,5,9,12}, -1));\n\t\tSystem.out.println(search(new int[]{-1,0,3,5,9,12}, 9));\n\t\tSystem.out.println(search(new int[]{-1,0,3,5,9,12}, 2));\n\t}\n\n\n\n}\n<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">704\u9898\u662f\u4e00\u4e2a\u57fa\u7840\u7248\u672c\uff0c\u8fd4\u56de\u7684\u662f\u4e0b\u6807\uff0c\u5b9a\u65f6low\u548chigh\u7528\u7684\u662f[0,n-1], \u5faa\u73af\u65f6\u5019\u662f&lt;=\u5224\u65ad<\/mark><\/p>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/first-bad-version\/\">278. \u7b2c\u4e00\u4e2a\u9519\u8bef\u7684\u7248\u672c<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**\n\n \u4f60\u662f\u4ea7\u54c1\u7ecf\u7406\uff0c\u76ee\u524d\u6b63\u5728\u5e26\u9886\u4e00\u4e2a\u56e2\u961f\u5f00\u53d1\u65b0\u7684\u4ea7\u54c1\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u4f60\u7684\u4ea7\u54c1\u7684\u6700\u65b0\u7248\u672c\u6ca1\u6709\u901a\u8fc7\u8d28\u91cf\u68c0\u6d4b\u3002\u7531\u4e8e\u6bcf\u4e2a\u7248\u672c\u90fd\u662f\u57fa\u4e8e\u4e4b\u524d\u7684\u7248\u672c\u5f00\u53d1\u7684\uff0c\u6240\u4ee5\u9519\u8bef\u7684\u7248\u672c\u4e4b\u540e\u7684\u6240\u6709\u7248\u672c\u90fd\u662f\u9519\u7684\u3002\n\n \u5047\u8bbe\u4f60\u6709 n \u4e2a\u7248\u672c [1, 2, ..., n]\uff0c\u4f60\u60f3\u627e\u51fa\u5bfc\u81f4\u4e4b\u540e\u6240\u6709\u7248\u672c\u51fa\u9519\u7684\u7b2c\u4e00\u4e2a\u9519\u8bef\u7684\u7248\u672c\u3002\n\n \u4f60\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 bool isBadVersion(version) \u63a5\u53e3\u6765\u5224\u65ad\u7248\u672c\u53f7 version \u662f\u5426\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u51fa\u9519\u3002\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u6765\u67e5\u627e\u7b2c\u4e00\u4e2a\u9519\u8bef\u7684\u7248\u672c\u3002\u4f60\u5e94\u8be5\u5c3d\u91cf\u51cf\u5c11\u5bf9\u8c03\u7528 API \u7684\u6b21\u6570\u3002\n\n\n \u793a\u4f8b 1\uff1a\n\n \u8f93\u5165\uff1an = 5, bad = 4\n \u8f93\u51fa\uff1a4\n \u89e3\u91ca\uff1a\n \u8c03\u7528 isBadVersion(3) -&gt; false\n \u8c03\u7528 isBadVersion(5) -&gt; true\n \u8c03\u7528 isBadVersion(4) -&gt; true\n \u6240\u4ee5\uff0c4 \u662f\u7b2c\u4e00\u4e2a\u9519\u8bef\u7684\u7248\u672c\u3002\n \u793a\u4f8b 2\uff1a\n\n \u8f93\u5165\uff1an = 1, bad = 1\n \u8f93\u51fa\uff1a1\n\n\n \u63d0\u793a\uff1a\n\n 1 &lt;= bad &lt;= n &lt;= 231 - 1\n\n\n \u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\n \u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/first-bad-version\n \u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002\n\n * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart\n *\n *\/<\/em>\n\n\n\npublic class Question278 {\n\tpublic int firstBadVersion(int n) {\n\t\tint low = 0;\n\t\tint high = n-1;\n\n\t\twhile (low &lt;= high){\n\t\t\t\/\/ \u9632\u6b62\u6ea2\u51fa\n\t\t\tint mid = low + (high - low) \/ 2;\n\t\t\t\/\/int mid = (high+low) \/ 2;\n\t\t\tif(isBadVersion(mid)){\n\t\t\t\thigh = mid - 1;\n\t\t\t}else {\n\t\t\t\tlow = mid + 1;\n\t\t\t}\n\t\t}\n\t\treturn low;\n\t}\n\n}<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">278\u9898\u662f\u53d8\u79cd\uff0c\u8fd4\u56de\u7684\u662f\u7b2c\u51e0\u4e2a\u5143\u7d20\uff0c\u6700\u7ec8\u8f93\u51falow\uff0c\u540c\u65f6\uff0c\u4e3a\u4e86\u9632\u6b62\u6ea2\u51fa\uff0cmid\u7684\u7b97\u6cd5\u6539\u4e3aint mid = low + (high &#8211; low) \/ 2<\/mark><\/p>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/search-insert-position\/\">35. \u641c\u7d22\u63d2\u5165\u4f4d\u7f6e<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/**\n \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\n\n \u8bf7\u5fc5\u987b\u4f7f\u7528\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(log n) \u7684\u7b97\u6cd5\u3002\n\n \u793a\u4f8b 1:\n\n \u8f93\u5165: nums = [1,3,5,6], target = 5\n \u8f93\u51fa: 2\n \u793a\u4f8b 2:\n\n \u8f93\u5165: nums = [1,3,5,6], target = 2\n \u8f93\u51fa: 1\n \u793a\u4f8b 3:\n\n \u8f93\u5165: nums = [1,3,5,6], target = 7\n \u8f93\u51fa: 4\n \u793a\u4f8b 4:\n\n \u8f93\u5165: nums = [1,3,5,6], target = 0\n \u8f93\u51fa: 0\n \u793a\u4f8b 5:\n\n \u8f93\u5165: nums = [1], target = 0\n \u8f93\u51fa: 0\n\n\n \u63d0\u793a:\n\n 1 &lt;= nums.length &lt;= 10^4\n -10^4 &lt;= nums[i] &lt;= 10^4\n nums \u4e3a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u5347\u5e8f\u6392\u5217\u6570\u7ec4\n -10^4 &lt;= target &lt;= 10^4\n\n \u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\n \u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/search-insert-position\n \u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002\n\n * @author zeroheart\n *\n *\/\npublic class Question35 {\n    public static int searchInsert(int[] nums, int target) {\n        int low = 0;\n        int high = nums.length - 1;\n\n        while (low &lt;= high){\n            int mid = low + (high - low) \/ 2;\n            if(nums[mid] == target){\n                return mid;\n            }else if(nums[mid]&gt;target) {\n                high = mid - 1;\n            }else {\n                low = mid + 1;\n            }\n        }\n        \/\/ \u5bfb\u627e\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u6240\u6709\u90fd\u548c\u57fa\u7840\u4e8c\u5206\u4e00\u6837\uff0c\u53ea\u6709\u8fd9\u91cc\u5728\u6240\u6709\u90fd\u65e0\u6cd5\u5339\u914d\u4e4b\u540e,\n        \/\/ \u56e0\u4e3a\u9000\u51fa\u65f6\u5019\uff0clow\u548chigh\u7684\u60c5\u51b5\u4e00\u5b9a\u662f\u4ee5\u4e0b\u60c5\u51b5\n        \/\/ \u76ee\u6807\u503c\u5728\u6570\u7ec4\u6240\u6709\u5143\u7d20\u4e4b\u524d  [0, -1]\n        \/\/ \u76ee\u6807\u503c\u7b49\u4e8e\u6570\u7ec4\u4e2d\u67d0\u4e00\u4e2a\u5143\u7d20  return middle;\n        \/\/ \u76ee\u6807\u503c\u63d2\u5165\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e [low, high]\n        \/\/ \u76ee\u6807\u503c\u5728\u6570\u7ec4\u6240\u6709\u5143\u7d20\u4e4b\u540e\u7684\u60c5\u51b5 [n+1, n]\n        \/\/ \u6240\u4ee5\uff0c\u6700\u540e\u8fd4\u56de high + 1 \u6216\u8005\u8fd4\u56de low\n        return high + 1;\n    }\n\n    public static void main(String args[]){\n        System.out.println(searchInsert(new int[]{-1,0,3,5,9,12}, -2));\n        System.out.println(searchInsert(new int[]{-1,0,3,5,9,12}, -1));\n        System.out.println(searchInsert(new int[]{-1,0,3,5,9,12}, 9));\n        System.out.println(searchInsert(new int[]{-1,0,3,5,9,12}, 2));\n    }\n\n}\n<\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">35\u9898\u4e3b\u8981\u662f\u627e\u4e00\u4e2a\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u4f7f\u7528\u4e8c\u5206\u6cd5\u7684\u8bdd\uff0c\u548c\u666e\u901a\u4e8c\u5206\u7684\u533a\u522b\u5c31\u5728\u6700\u540e\u4e00\u884c\u8fd4\u56de\u503c\u7684\u4f4d\u7f6e\uff0creturn high + 1;<\/mark><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e8c\u5206\u67e5\u627e \u300c\u7b97\u6cd5\u300d &#8211; \u5b66\u4e60\u8ba1\u5212 &#8211; \u529b\u6263\uff08LeetCode\uff09\u5168\u7403\u6781\u5ba2\u631a\u7231\u7684\u6280\u672f\u6210\u957f\u5e73 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[156],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2403"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2403"}],"version-history":[{"count":17,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2403\/revisions"}],"predecessor-version":[{"id":2473,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2403\/revisions\/2473"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}