{"id":149662,"date":"2020-08-31T09:11:54","date_gmt":"2020-08-31T01:11:54","guid":{"rendered":"http:\/\/4563.org\/?p=149662"},"modified":"2020-08-31T09:11:54","modified_gmt":"2020-08-31T01:11:54","slug":"lintcode-%e9%a2%86%e6%89%a3-%e9%a2%98%e8%a7%a3%e4%b8%a8%e5%be%ae%e8%bd%af%e9%ab%98%e9%a2%91%e9%a2%98%ef%bc%9a%e6%90%9c%e7%b4%a2%e6%97%8b%e8%bd%ac%e6%8e%92%e5%ba%8f%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=149662","title":{"rendered":"LintCode \u9886\u6263 \u9898\u89e3\u4e28\u5fae\u8f6f\u9ad8\u9891\u9898\uff1a\u641c\u7d22\u65cb\u8f6c\u6392\u5e8f\u6570\u7ec4"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  LintCode \u9886\u6263 \u9898\u89e3\u4e28\u5fae\u8f6f\u9ad8\u9891\u9898\uff1a\u641c\u7d22\u65cb\u8f6c\u6392\u5e8f\u6570\u7ec4               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakunamatata11 <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u6392\u5e8f\u7684\u6309\u672a\u77e5\u7684\u65cb\u8f6c\u8f74\u65cb\u8f6c\u7684\u6570\u7ec4(\u6bd4\u5982\uff0c0 1 2 4 5 6 7 \u53ef\u80fd\u6210\u4e3a 4 5 6 7 0 1 2)\u3002\u7ed9\u5b9a\u4e00\u4e2a\u76ee\u6807\u503c\u8fdb\u884c\u641c\u7d22\uff0c\u5982\u679c\u5728\u6570\u7ec4\u4e2d\u627e\u5230\u76ee\u6807\u503c\u8fd4\u56de\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u5426\u5219\u8fd4\u56de-1 \u3002\u4f60\u53ef\u4ee5\u5047\u8bbe\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n<p>\u770b\u9898\u89e3\u4e4b\u524d\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u2192\u5728\u7ebf\u505a\u9898<\/p>\n<h2>\u4f8b 1:<\/h2>\n<pre><code>\u8f93\u5165: [4, 5, 1, 2, 3] and target=1,  \u8f93\u51fa: 2.  <\/code><\/pre>\n<h2>\u4f8b 2:<\/h2>\n<pre><code>\u8f93\u5165: [4, 5, 1, 2, 3] and target=0,  \u8f93\u51fa: -1.  <\/code><\/pre>\n<h1>[\u9898\u89e3]<\/h1>\n<p> <\/p>\n<h2>\u7b97\u6cd5\uff1a\u4e8c\u5206<\/h2>\n<ul>\n<li>\u6839\u636e\u9898\u76ee\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u65cb\u8f6c\u6570\u7ec4\u5b9e\u9645\u4e0a\u662f\u4e24\u4e2a\u9012\u589e\u6570\u7ec4\u7684\u7ec4\u6210\uff0c\u4e14\u7b2c\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u6700\u5c0f\u503c\u5927\u4e8e\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u7684\u6700\u5927\u503c<\/li>\n<li>\u7531\u4e8e\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u91cd\u590d\u7684\u5143\u7d20\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5148\u627e\u5230 target \u5728\u54ea\u4e2a\u6570\u7ec4\uff0c\u518d\u8fdb\u884c\u4e8c\u5206<\/li>\n<\/ul>\n<h2>\u4ee3\u7801\u601d\u8def<\/h2>\n<ul>\n<li>\u4e8c\u5206\u627e\u5230\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u5373\u6574\u4e2a\u6570\u7ec4\u7684\u6700\u5c0f\u503c\u7684\u4f4d\u7f6e minPosition<\/li>\n<li>\u901a\u8fc7\u6bd4\u8f83 target \u548c\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u6700\u5c0f\u5143\u7d20\uff08\u5373\u6700\u540e\u4e00\u4e2a\u6570\uff09\u5927\u5c0f\u5173\u7cfb\u5224\u65ad target \u5728\u54ea\u4e00\u4e2a\u6570\u7ec4<\/li>\n<li>\u5bf9 target \u6240\u5728\u7684\u6570\u7ec4\u4e8c\u5206<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<p>N \u8868\u793a\u4e3a A \u6570\u7ec4\u7684\u957f\u5ea6<\/p>\n<ul>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(N)<\/li>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(logN)<\/li>\n<\/ul>\n<pre><code>public class Solution {     \/**      * @param A: an integer rotated sorted array      * @param target: an integer to be searched      * @return: an integer      *\/     public int search(int[] A, int target) {         if (A == null || A.length == 0) {              return -1;          }           \/\/\u627e\u5230\u6570\u7ec4\u6700\u5c0f\u503c\u4f4d\u7f6e minPosition\uff0c\u5373\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u7684\u8d77\u59cb\u4f4d\u7f6e         int minPosition = 0;          intleft = 0;          int right = A.length - 1;          while (left + 1 &lt; right) {              int mid = left + (right - left) \/ 2;              if (A[mid] &gt; A[right]) {                 left = mid;              } else {                  right = mid;              }          }                   if (A[left] &lt; A[right]) {              minPosition = left;          } else {              minPosition = right;          }           \/\/\u5224\u65ad target \u5728\u54ea\u4e00\u4e2a\u6570\u7ec4\u4e2d         if (A[A.length - 1] &lt; target) {              left = 0;              right = minPosition - 1;          } else {              left = minPosition;              right = A.length - 1;          }          \/\/\u5bf9 target \u6240\u5728\u6570\u7ec4\u4e8c\u5206\u641c\u7d22         while (left + 1 &lt; right) {              int mid = left + (right - left) \/ 2;              if (A[mid] &lt; target) {                  left = mid;              } else {                  right = mid;              }          }                    if (A[left] == target) {              return left;          }          if (A[right] == target) {              return right;          }                  return -1;      } }  <\/code><\/pre>\n<p>\u70b9\u6b64\u5904\u67e5\u770b\u66f4\u591a\u9898\u89e3<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>LintCode \u9886\u6263 \u9898\u89e3\u4e28\u5fae\u8f6f&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\/149662"}],"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=149662"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/149662\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149662"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}