{"id":157925,"date":"2020-09-18T02:03:23","date_gmt":"2020-09-17T18:03:23","guid":{"rendered":"http:\/\/4563.org\/?p=157925"},"modified":"2020-09-18T02:03:23","modified_gmt":"2020-09-17T18:03:23","slug":"leetcode-lintcode-%e9%a2%98%e8%a7%a3-%e7%ae%97%e6%b3%95%e6%a8%a1%e6%9d%bf%ef%bc%9a%e4%ba%8c%e5%88%86%e6%9f%a5%e6%89%be","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=157925","title":{"rendered":"[leetcode\/lintcode \u9898\u89e3] \u7b97\u6cd5\u6a21\u677f\uff1a\u4e8c\u5206\u67e5\u627e"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [leetcode\/lintcode \u9898\u89e3] \u7b97\u6cd5\u6a21\u677f\uff1a\u4e8c\u5206\u67e5\u627e               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6392\u5e8f\u7684\u6574\u6570\u6570\u7ec4\uff08\u5347\u5e8f\uff09\u548c\u4e00\u4e2a\u8981\u67e5\u627e\u7684\u6574\u6570 target\uff0c\u7528 O(logn)\u7684\u65f6\u95f4\u67e5\u627e\u5230 target \u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u4e0b\u6807\uff08\u4ece 0 \u5f00\u59cb\uff09\uff0c\u5982\u679c target \u4e0d\u5b58\u5728\u4e8e\u6570\u7ec4\u4e2d\uff0c\u8fd4\u56de-1 \u3002<\/p>\n<p>\u5728\u7ebf\u505a\u9898\u5730\u5740<\/p>\n<h2>\u6837\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165:[1,4,4,5,7,7,8,9,9,10]\uff0c1 \u8f93\u51fa: 0          \u6837\u4f8b\u89e3\u91ca:  \u7b2c\u4e00\u6b21\u51fa\u73b0\u5728\u7b2c 0 \u4e2a\u4f4d\u7f6e\u3002 <\/code><\/pre>\n<h2>\u6837\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165: [1, 2, 3, 3, 4, 5, 10]\uff0c3 \u8f93\u51fa: 2          \u6837\u4f8b\u89e3\u91ca:  \u7b2c\u4e00\u6b21\u51fa\u73b0\u5728\u7b2c 2 \u4e2a\u4f4d\u7f6e <\/code><\/pre>\n<h2>\u6837\u4f8b 3:<\/h2>\n<pre><code>\u8f93\u5165: [1, 2, 3, 3, 4, 5, 10]\uff0c6 \u8f93\u51fa: -1          \u6837\u4f8b\u89e3\u91ca:  \u6ca1\u6709\u51fa\u73b0\u8fc7 6\uff0c \u8fd4\u56de-1 <\/code><\/pre>\n<h2>\u89e3\u9898\u601d\u8def<\/h2>\n<p>\u9898\u76ee\u63d0\u5230\uff0c\u7ed9\u5b9a\u7684\u6570\u7ec4\u5df2\u7ecf\u6392\u5e8f\uff0c\u82e5\u4ece\u5c0f\u5230\u5927\u904d\u5386\u6570\u7ec4\u67e5\u627e target\uff0c\u5219\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)O(n)\uff0cn \u4e3a\u6570\u7ec4\u957f\u5ea6\u3002\u9700\u8981\u7528\u4e00\u4e2a O(logn)O(logn)\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53bb\u5b8c\u6210\u672c\u9898\uff0c\u90a3\u4e48\u9700\u8981\u7528\u5230\u4e8c\u5206\u67e5\u627e\u3002<\/p>\n<p>\u4e8c\u5206\u67e5\u627e\u5e38\u7528\u4e8e\u67e5\u627e\u6709\u5e8f\u6570\u7ec4\u4e2d\u76ee\u6807\u6570 target \u7684\u4f4d\u7f6e\uff0c\u7528 left \u548c right \u8bb0\u5f55 target \u6240\u5728\u7684\u533a\u95f4\u7aef\u70b9\uff0c\u6bcf\u6b21\u5c06\u533a\u95f4\u7684\u4e2d\u95f4\u4f4d\u7f6e\u503c\u548c target \u4f5c\u6bd4\u8f83\uff0c\u7136\u540e\u79fb\u52a8\u533a\u95f4\u7aef\u70b9\u3002<\/p>\n<h2>\u7b97\u6cd5\u6d41\u7a0b<\/h2>\n<ul>\n<li>\u5c06\u533a\u95f4\u8d4b\u503c\u4e3a\u6574\u4e2a\u6570\u7ec4\u533a\u95f4\uff08 left = 0, right = n &#8211; 1 \uff09\uff0c\u53d6\u4e2d\u95f4\u4f4d\u7f6e mid<\/li>\n<li>\u82e5 a[mid] &lt; target\uff0c\u5219\u5c06\u533a\u95f4\u7f29\u5c0f\u5230\u539f\u533a\u95f4\u7684\u53f3\u533a\u95f4(left = mid + 1)<\/li>\n<li>\u82e5 a[mid] &gt;= target\uff0c\u5219\u5c06\u533a\u95f4\u7f29\u5c0f\u81f3\u539f\u533a\u95f4\u7684\u5de6\u533a\u95f4(right = mid)<\/li>\n<li>\u82e5 left &gt;= right \u65f6\uff0c\u82e5 a[right] = target \u5219\u8fd4\u56de right, \u5426\u5219\u8fd4\u56de-1<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(logn)<\/li>\n<li>\u6bcf\u6b21\u67e5\u627e\u90fd\u5c06\u533a\u95f4\u7f29\u5c0f\u81f3\u539f\u6765\u957f\u5ea6\u7684\u4e00\u534a\uff0c\u53ef\u89c1\u67e5\u627e\u7684\u6700\u591a\u6b21\u6570\u4e3a logn<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(1)<\/li>\n<li>\u67e5\u627e\u4e0d\u9700\u8981\u5f00\u8f9f\u65b0\u7684\u975e\u5e38\u6570\u7ea7\u7a7a\u95f4\uff0c\u53ea\u9700\u5728\u539f\u6570\u7ec4\u57fa\u7840\u4e0a\u8fdb\u884c\u67e5\u627e\u5373\u53ef<\/li>\n<\/ul>\n<h2>\u4ee3\u7801<\/h2>\n<pre><code>public class Solution {     \/**      * @param nums: The integer array.      * @param target: Target to find.      * @return: The first position of target. Position starts from 0.      *\/     public int binarySearch(int[] nums, int target) {         int left = 0;         int right = nums.length - 1;         int mid;          while (left &lt; right) {             \/\/\u5f97\u5230\u4e2d\u95f4\u4f4d\u7f6e             mid = (right + left) \/ 2;             if (nums[mid] &lt; target) {                 left = mid + 1;             } else {                 right = mid;             }         }          if (nums[right] == target) {             return right;         }          return -1;     } }  <\/code><\/pre>\n<p>\u70b9\u6b64\u67e5\u770b\u66f4\u591a\u9898\u89e3<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3376694\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : slipper <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1.\u9898\u76ee\u8ba9\u4f60\u8003\u8651\u6ea2\u51fa\uff0c\u4f60\u6ca1\u6709\u8003\u8651\u3002<br \/>2. left &lt; right \u548c right=mid \u6709\u5361\u6b7b\u7684\u98ce\u9669\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3376695\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : eddycoding <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u8c22\u5206\u4eab                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[leetcode\/lintcod&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/157925"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157925"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/157925\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157925"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}