{"id":186,"date":"2022-02-26T19:32:00","date_gmt":"2022-02-26T11:32:00","guid":{"rendered":"http:\/\/nas.hepcloud.top:7000\/?p=186"},"modified":"2022-02-26T19:32:00","modified_gmt":"2022-02-26T11:32:00","slug":"4-%e5%af%bb%e6%89%be%e4%b8%a4%e4%b8%aa%e6%ad%a3%e5%ba%8f%e6%95%b0%e7%bb%84%e7%9a%84%e4%b8%ad%e4%bd%8d%e6%95%b0","status":"publish","type":"post","link":"http:\/\/www.huerpu.cc:7000\/?p=186","title":{"rendered":"4. \u5bfb\u627e\u4e24\u4e2a\u6b63\u5e8f\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e24\u4e2a\u5927\u5c0f\u5206\u522b\u4e3a<code>m<\/code>\u548c<code>n<\/code>\u7684\u6b63\u5e8f(\u4ece\u5c0f\u5230\u5927)\u6570\u7ec4<code>nums1<\/code>\u548c<code>nums2<\/code>\u3002\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u8fd9\u4e24\u4e2a\u6b63\u5e8f\u6570\u7ec4\u7684<strong>\u4e2d\u4f4d\u6570<\/strong>\u3002<\/p>\n\n\n<p>\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5e94\u8be5\u4e3a<code>O(log(m+n))<\/code>\u3002<\/p>\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n<pre class=\"wp-block-preformatted\">\u8f93\u5165\uff1anums1 = [1,3], nums2 = [2]<br>\u8f93\u51fa\uff1a2.00000<br>\u89e3\u91ca\uff1a\u5408\u5e76\u6570\u7ec4 = [1,2,3] \uff0c\u4e2d\u4f4d\u6570 2<\/pre>\n\n\n<p><strong><br>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n<pre class=\"wp-block-preformatted\">\u8f93\u5165\uff1anums1 = [1,2], nums2 = [3,4]<br>\u8f93\u51fa\uff1a2.50000<br>\u89e3\u91ca\uff1a\u5408\u5e76\u6570\u7ec4 = [1,2,3,4] \uff0c\u4e2d\u4f4d\u6570(2+3)\/2=2.5<\/pre>\n\n\n<p><strong><br>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n<ul class=\"wp-block-list\"><li>nums1.length == m<\/li><li>nums2.length == n<\/li><li>0 &lt;= m &lt;= 1000<\/li><li>0 &lt;= n &lt;= 1000<\/li><li>1 &lt;= m + n &lt;= 2000<\/li><li>-10<sup>6<\/sup> &lt;= nums1[i], nums2[i] &lt;= 10<sup>6<\/sup><\/li><\/ul>\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u57fa\u7840\u601d\u60f3\u89e3\u6cd5<\/h3>\n\n\n<p>\u904d\u5386\u4e24\u4e2a\u6570\u7ec4\uff0c\u5c31\u662f\u4e00\u770b\u5230\u9898\u76ee\u5c31\u60f3\u5230\u7684\u90a3\u79cd\u7b97\u6cd5\u601d\u60f3\uff0c\u6761\u4ef6\u5f88\u591a\uff0c\u53c8\u81ed\u53c8\u957f\u3002\u65f6\u95f4\u590d\u6742\u5ea6: <code>O(m+n)<\/code>, \u7a7a\u95f4\u590d\u6742\u5ea6: <code>O(1)<\/code>\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">public &nbsp;double findMedianSortedArrays(int[] nums1, int[] nums2) {<br> &nbsp; &nbsp;double median = 0.0;<br> &nbsp; &nbsp;int m = nums1.length, n = nums2.length;<br> &nbsp; &nbsp;\/\/\u5206\u522b\u6307\u5411\u6570\u7ec4nums1\u548cnums2\u7684\u4e0b\u6807<br> &nbsp; &nbsp;int mIdx = 0, nIdx = 0,temIdx = 0;<br> &nbsp; &nbsp;\/\/\u4e2d\u4f4d\u6570\u7684\u4f4d\u7f6e<br> &nbsp; &nbsp;int desIdx = (m + n + 1) \/ 2;<br> &nbsp; &nbsp;\/\/\u662f\u5426\u4e3a\u4e24\u4e2a\u6570\u7684\u4e2d\u4f4d\u6570<br> &nbsp; &nbsp;boolean isTwo = (m + n) % 2 == 0;<br>\u200b<br> &nbsp; &nbsp;while(temIdx &lt;= desIdx || isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp;temIdx++;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u4e24\u4e2a\u6570\u7ec4\u8fdb\u884c\u6bd4\u8f83\uff0c\u53d6\u6700\u5c0f\u7684\u90a3\u4e2a<br> &nbsp; &nbsp; &nbsp; &nbsp;if(mIdx &lt; m &amp;&amp; nIdx &lt; n &amp;&amp; nums1[mIdx] &lt;= nums2[nIdx]){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(temIdx == desIdx &amp;&amp; !isTwo){\/\/\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u4e2d\u4f4d\u6570\uff0c\u627e\u5230\u76f4\u63a5\u8fd4\u56de<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nums1[mIdx];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx &amp;&amp; isTwo){\/\/\u5982\u679c\u6709\u4e24\u4e2a\u4e2d\u4f4d\u6570\uff0c\u5e76\u4e14\u73b0\u5728\u6307\u5411\u4e86\u7b2c\u4e00\u4e2a\u6570\uff0c\u628a\u5b83\u8d4b\u7ed9media<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;median = nums1[mIdx];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx + 1){\/\/\u5982\u679c\u6709\u4e24\u4e2a\u4e2d\u4f4d\u6570\uff0c\u5e76\u4e14\u73b0\u5728\u6307\u5411\u4e86\u7b2c\u4e8c\u4e2a\u6570\uff0c\u8fd4\u56de\u7ed3\u679c<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (median + nums1[mIdx]) \/ 2.0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mIdx++;<br> &nbsp; &nbsp; &nbsp;  }else if(mIdx &lt; m &amp;&amp; nIdx &lt; n &amp;&amp; nums1[mIdx] &gt; nums2[nIdx]){\/\/\/\/\u4e24\u4e2a\u6570\u7ec4\u8fdb\u884c\u6bd4\u8f83\uff0c\u53d6\u6700\u5c0f\u7684\u90a3\u4e2a<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(temIdx == desIdx &amp;&amp; !isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nums2[nIdx];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx &amp;&amp; isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;median = nums2[nIdx];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx + 1){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (median + nums2[nIdx]) \/ 2.0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nIdx++;<br> &nbsp; &nbsp; &nbsp;  }else if(mIdx &lt; m &amp;&amp; nIdx == n){\/\/\u5f53\u6570\u7ec4num2\u5df2\u7ecf\u904d\u5386\u5b8c\uff0c\u800c\u6570\u636enums1\u8fd8\u6709\u6570\u636e<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(temIdx == desIdx &amp;&amp; !isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nums1[mIdx];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx &nbsp;&amp;&amp; isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (nums1[mIdx] + nums1[++mIdx]) \/ 2.0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx + 1 &nbsp;&amp;&amp; isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (median + nums1[mIdx]) \/ 2.0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mIdx++;<br> &nbsp; &nbsp; &nbsp;  }else if(nIdx &lt; n &amp;&amp; mIdx == m){\/\/\u5f53\u6570\u7ec4num1\u5df2\u7ecf\u904d\u5386\u5b8c\uff0c\u800c\u6570\u636enums2\u8fd8\u6709\u6570\u636e<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(temIdx == desIdx &amp;&amp; !isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nums2[nIdx];<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx &amp;&amp; isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (nums2[nIdx] + nums2[++nIdx]) \/ 2.0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }else if(temIdx == desIdx + 1 &amp;&amp; isTwo){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return  (median + nums2[nIdx]) \/ 2.0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nIdx++;<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp;  }<br> &nbsp; &nbsp;return median;<br>}<\/pre>\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u7cbe\u7b80\u89e3\u6cd5<\/h3>\n\n\n<p>\u7528m\u8868\u793a\u7b2c\u4e00\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\uff0cn\u8868\u793a\u7b2c\u4e8c\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5982\u679c\u603b\u957f\u5ea6\u4e3a\u5947\u6570\uff0c\u6bd4\u5982m=2\uff0cn=3\uff0c\u90a3\u4e48\u5faa\u73af\u5230<code>(m+n)\/2 + 1=(2+3)\/2 + 1=3<\/code>\uff0c\u4e5f\u5c31\u662f\u53d6\u7b2c\u4e09\u4e2a\u6570\uff0c\u6982\u62ec\u6765\u8bf4\u662f\u7b2c<code>(m+n)\/2 + 1<\/code>\u4e2a\u6570\u3002\u5982\u679c\u603b\u957f\u5ea6\u4e3a\u5076\u6570\uff0c\u6bd4\u5982m=2\uff0cn=2\uff0c\u90a3\u4e48\u5faa\u73af\u5230<code>(m+n)\/2 + 1=(2+2)\/2 + 1=3<\/code>\uff0c\u53d6\u7b2c2\u4e2a\u548c\u7b2c3\u4e2a\u6570\uff0c\u6982\u62ec\u6765\u8bf4\u662f\u7b2c<code>(m+n)\/2<\/code>\u548c<code>(m+n)\/2 + 1<\/code>\u4e2a\u6570\u3002\u4f7f\u7528\u4e24\u4e2a\u53d8\u91cfpre\u548ccur\uff0cpre\u8868\u793a\u4e0a\u4e00\u6b21\u5faa\u73af\u4e2d\u627e\u5230\u7684\u6570\uff0ccur\u8868\u793a\u5f53\u524d\u5faa\u73af\u627e\u5230\u7684\u6570\uff0c\u6bcf\u6b21\u5728\u5f53\u524d\u5faa\u73af\u90fd\u628acur\u8d4b\u7ed9pre\uff0c\u8fd9\u6837\u5230\u6700\u540e\u4e00\u6b21\u627e\u5230\u7684\u7b2c<code>(m+n)\/2<\/code>\u548c<code>(m+n)\/2 + 1<\/code>\u4e2a\u6570\u5c31\u662fpre\u548ccur\uff0c\u5982\u679c\u603b\u957f\u5ea6\u4e3a\u5947\u6570\u76f4\u63a5\u53d6\u5f53\u524dcur\u5373\u53ef\u3002<\/p>\n\n\n<p>\u5bf9\u4e8e\u4e24\u4e2a\u6570\u7ec4\u5206\u522b\u7528mIdx\u548cnIdx\u6807\u793a\u6570\u7ec4\u7684\u4e0b\u6807\uff0c\u5982\u679cnums2\u6570\u7ec4\u6b64\u523b\u5df2\u7ecf\u6ca1\u6709\u6570\u5b57\u4e86\uff0c\u7ee7\u7eed\u53d6\u6570\u5b57 <code>nums2[nIdx]<\/code>\uff0c\u5219\u4f1a\u8d8a\u754c\uff0c\u6240\u4ee5\u5224\u65adnIdx\u662f\u5426\u5927\u4e8e\u6570\u7ec4\u957f\u5ea6\uff0c\u8fd9\u6837 || \u540e\u8fb9\u7684\u5c31\u4e0d\u4f1a\u6267\u884c\u4e86\uff0c\u4e5f\u5c31\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8bef\u4e86\uff0c\u6240\u4ee5\u589e\u52a0\u4e3a<code>mIdx &lt; m &amp;&amp; (nIdx &gt;= n || nums1[mIdx] &lt; nums2[nIdx])<\/code>\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">public double findMedianSortedArrays(int[] nums1, int[] nums2) {<br> &nbsp; &nbsp;int m = nums1.length,n = nums2.length;<br> &nbsp; &nbsp;int mIdx = 0, nIdx = 0;<br> &nbsp; &nbsp;\/\/\u5faa\u73af\u4e24\u4e2a\u6570\u7ec4\uff0c\u6309\u4ece\u5c0f\u5230\u5927\u53bb\u83b7\u53d6\uff0c\u7136\u540e\u627e\u5230\u4e2d\u95f4\u70b9\u7684\u6570\uff0c\u6216\u8005\u4e2d\u95f4\u4e24\u4e2a\u6570\uff0c\u5728\u5bfb\u627e\u7684\u8fc7\u7a0b\u4e2d\uff0cpre\u8bb0\u5f55\u4e0a\u4e00\u4e2a\u503c\uff0ccur\u8bb0\u5f55\u5f53\u524d\u503c\u3002<br> &nbsp; &nbsp;int pre = 0, cur = 0;<br> &nbsp; &nbsp;\/\/i\u4ece0\u5230(m + n) \/ 2\uff0c\u5f53m + n\u4e3a\u5076\u6570\u65f6\uff0ci\u5faa\u73af\u5230\u4e2d\u95f4\u4e24\u4e2a\u6570\u7684\u540e\u4e00\u4e2a\uff0c\u5f53m + n\u4e3a\u5947\u6570\u65f6\uff0ci\u5faa\u73af\u5230\u4e2d\u95f4\u90a3\u4e2a\u6570<br> &nbsp; &nbsp;for(int i = 0; i &lt; (m + n) \/ 2 + 1; i++){<br> &nbsp; &nbsp; &nbsp; &nbsp;pre = cur;<br> &nbsp; &nbsp; &nbsp; &nbsp;if(mIdx &lt; m &amp;&amp; (nIdx &gt;= n || nums1[mIdx] &lt; nums2[nIdx])){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur = nums1[mIdx++];<br> &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur = nums2[nIdx++];<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;\/\/\u4e24\u4e2a\u6570\u7684\u65f6\u5019\uff0c\u53d6pre\u548ccur\u7684\u5e73\u5747<br> &nbsp; &nbsp;if((m + n) % 2 == 0){<br> &nbsp; &nbsp; &nbsp; &nbsp;return (pre + cur) \/ 2.0;<br> &nbsp;  }else{\/\/\u5426\u5219\u8fd4\u56decur<br> &nbsp; &nbsp; &nbsp; &nbsp;return cur;<br> &nbsp;  }<br>\u200b<br>}<\/pre>\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u4e8c\u5206\u67e5\u627e\u89e3\u6cd5<\/h3>\n\n\n<p>\u6839\u636e\u4e2d\u4f4d\u6570\u7684\u5b9a\u4e49\uff0c\u5f53<code>m+n<\/code>\u662f\u5947\u6570\u65f6\uff0c\u4e2d\u4f4d\u6570\u662f\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u7b2c<code>(m+n)\/2+1<\/code>\u4e2a\u5143\u7d20\uff0c\u5f53<code>m+n<\/code>\u662f\u5076\u6570\u65f6\uff0c\u4e2d\u4f4d\u6570\u662f\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u7b2c<code>(m+n)\/2<\/code>\u4e2a\u5143\u7d20\u548c\u7b2c<code>(m+n)\/2+1<\/code>\u4e2a\u5143\u7d20\u7684\u5e73\u5747\u503c\u3002\u56e0\u6b64\uff0c\u8fd9\u9053\u9898\u53ef\u4ee5\u8f6c\u5316\u6210\u5bfb\u627e\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u7b2ck\u5c0f\u7684\u6570\uff0c\u5176\u4e2dk\u4e3a<code>(m+n)\/2<\/code>\u6216<code>(m+n)\/2+1<\/code>\u3002<\/p>\n\n\n<p>\u5047\u8bbe\u4e24\u4e2a\u6709\u5e8f\u6570\u7ec4\u5206\u522b\u662f A \u548c B\u3002\u8981\u627e\u5230\u7b2c k \u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u6bd4\u8f83 <code>A[k\/2\u22121]<\/code> \u548c <code>B[k\/2\u22121]<\/code>\uff0c\u5176\u4e2d\/\u8868\u793a\u6574\u6570\u9664\u6cd5\u3002\u7531\u4e8e<code>A[k\/2\u22121]<\/code> \u548c<code>B[k\/2\u22121]<\/code> \u7684\u524d\u9762\u5206\u522b\u6709 <code>A[0...k\/2\u22122]<\/code>\u548c <code>B[0...k\/2\u22122]<\/code>\uff0c\u5373 <code>k\/2\u22121<\/code> \u4e2a\u5143\u7d20\uff0c\u5bf9\u4e8e <code>A[k\/2\u22121]<\/code>\u548c <code>B[k\/2\u22121]<\/code>\u4e2d\u7684\u8f83\u5c0f\u503c\uff0c\u6700\u591a\u53ea\u4f1a\u6709<code>(k\/2-1)+(k\/2-1) = k\u22122<\/code>\u4e2a\u5143\u7d20\u6bd4\u5b83\u5c0f\uff0c\u90a3\u4e48\u5b83\u5c31\u4e0d\u80fd\u662f\u7b2c k \u5c0f\u7684\u6570\u4e86\u3002<\/p>\n\n\n<p>\u6709\u4ee5\u4e0b\u4e09\u79cd\u60c5\u51b5\u9700\u8981\u7279\u6b8a\u5904\u7406\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li>\u5982\u679c<code>A[k\/2\u22121]<\/code>\u6216\u8005 <code>B[k\/2\u22121]<\/code>\u8d8a\u754c\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u9009\u53d6\u5bf9\u5e94\u6570\u7ec4\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u6839\u636e\u6392\u9664\u6570\u7684\u4e2a\u6570\u51cf\u5c11k\u7684\u503c\uff0c\u800c\u4e0d\u80fd\u76f4\u63a5\u5c06<code>k<\/code>\u51cf\u53bb<code>k\/2-1<\/code>\u3002<\/li><li>\u5982\u679c\u4e00\u4e2a\u6570\u7ec4\u4e3a\u7a7a\uff0c\u8bf4\u660e\u8be5\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u88ab\u6392\u9664\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7b2c k \u5c0f\u7684\u5143\u7d20\u3002<\/li><li>\u5982\u679ck=1\uff0c\u6211\u4eec\u53ea\u8981\u8fd4\u56de\u4e24\u4e2a\u6570\u7ec4\u9996\u5143\u7d20\u7684\u6700\u5c0f\u503c\u5373\u53ef\u3002<\/li><\/ul>\n\n\n<p>\u4e3b\u8981\u601d\u8def\uff1a\u8981\u627e\u5230\u7b2c <code>k(k&gt;1)<\/code> \u5c0f\u7684\u5143\u7d20\uff0c\u90a3\u4e48\u5c31\u53d6 <code>A[k\/2-1]<\/code> \u548c <code>B[k\/2-1]<\/code>\u8fdb\u884c\u6bd4\u8f83\u3002A \u4e2d\u5c0f\u4e8e\u7b49\u4e8e <code>A[k\/2-1]<\/code>\u7684\u5143\u7d20\u6709 <code>A[0 .. k\/2-2]<\/code>\u5171\u8ba1 <code>k\/2-1<\/code>\u4e2a\uff0cB \u4e2d\u5c0f\u4e8e\u7b49\u4e8e <code>B[k\/2-1]<\/code>\u7684\u5143\u7d20\u6709<code>B[0 .. k\/2-2]<\/code>\u5171\u8ba1<code>k\/2-1<\/code>\u4e2a\u3002\u53d6 <code>min(A[k\/2-1], B[k\/2-1])<\/code>\uff0c\u4e24\u4e2a\u6570\u7ec4\u4e2d\u5c0f\u4e8e\u7b49\u4e8e <code>min(A[k\/2-1], B[k\/2-1])<\/code>\u7684\u5143\u7d20\u5171\u8ba1\u4e0d\u4f1a\u8d85\u8fc7 <code>(k\/2-1) + (k\/2-1) = k-2<\/code>\u4e2a\uff0c\u8fd9\u6837 <code>min(A[k\/2-1], B[k\/2-1])<\/code> \u672c\u8eab\u6700\u5927\u4e5f\u53ea\u80fd\u662f\u7b2c k-1 \u5c0f\u7684\u5143\u7d20\u3002 \u5982\u679c <code>A[k\/2-1] &lt;= B[k\/2-1]<\/code>\uff0c\u90a3\u4e48 <code>A[0 ... k\/2-1]<\/code>\u90fd\u4e0d\u53ef\u80fd\u662f\u7b2c k \u5c0f\u7684\u5143\u7d20\u3002\u628a\u8fd9\u4e9b\u5143\u7d20\u5168\u90e8 &quot;\u5220\u9664&quot;\uff0c\u5269\u4e0b\u7684\u4f5c\u4e3a\u65b0\u7684 A \u6570\u7ec4\uff1b\u5982\u679c <code>A[k\/2-1] &gt; B[k\/2-1]<\/code>\uff0c\u90a3\u4e48 <code>B[0 ... k\/2-1]<\/code>\u90fd\u4e0d\u53ef\u80fd\u662f\u7b2c k \u5c0f\u7684\u5143\u7d20\u3002\u628a\u8fd9\u4e9b\u5143\u7d20\u5168\u90e8 &quot;\u5220\u9664&quot;\uff0c\u5269\u4e0b\u7684\u4f5c\u4e3a\u65b0\u7684 B \u6570\u7ec4\u3002\u7531\u4e8e\u6211\u4eec &quot;\u5220\u9664&quot; \u4e86\u4e00\u4e9b\u5143\u7d20(\u8fd9\u4e9b\u5143\u7d20\u90fd\u6bd4\u7b2c k \u5c0f\u7684\u5143\u7d20\u8981\u5c0f)\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u6539 k \u7684\u503c\uff0c\u51cf\u53bb\u5220\u9664\u7684\u6570\u7684\u4e2a\u6570(k\/2-1)\u3002&quot;\u5220\u9664&quot;\u662f\u5728indxA\u548cindxB\u4e3a\u57fa\u7ebf\uff0c\u6bd4\u5982<code>A[0 ... k\/2-1]<\/code>\u5c31\u662f<code>A[indxA ... indxA + k\/2-1]<\/code>,<code>B[0 ... k\/2-1]<\/code>\u5c31\u662f<code>B[indxB ... indxB + k\/2-1]<\/code>\uff0c\u5220\u9664\u65f6\u66f4\u65b0indxA\u548cindxB\u5373\u53ef\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">\/\/3.\u91c7\u7528\u4e8c\u5206\u67e5\u627e<br>\/\/\u65f6\u95f4\u590d\u6742\u5ea6: O(log(m+n)), \u7a7a\u95f4\u590d\u6742\u5ea6: O(1)<br>public double findMedianSortedArrays(int[] A, int[] B) {<br> &nbsp; &nbsp;int lenA = A.length, lenB = B.length;<br> &nbsp; &nbsp;int len = lenA + lenB;<br> &nbsp; &nbsp;\/\/\u5982\u679c\u662f\u4e2d\u95f4\u4e00\u4e2a\u6570\u4e3a\u4e2d\u4f4d\u6570<br> &nbsp; &nbsp;if (len % 2 == 1) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u6bd4\u5982lenA = 2\uff0clenB = 3\uff0c\u4e00\u51715\u4e2a\u6570\u90a3\u5c31\u53d6\u7b2c3\u4e2a\u6570\uff0c\u4e5f\u5c31\u662f(lenA + lenB) \/ 2 + 1<br> &nbsp; &nbsp; &nbsp; &nbsp;int midIndx = len \/ 2 + 1;<br> &nbsp; &nbsp; &nbsp; &nbsp;return getKthElement(A, B, midIndx);<br> &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5982\u679c\u662f\u4e2d\u95f4\u4e24\u4e2a\u6570\u4e3a\u4e2d\u4f4d\u6570\uff0c\u6bd4\u5982lenA = 2\uff0clenB = 2\uff0c\u4e00\u5171\u56db\u4e2a\u6570\uff0c\u90a3\u5c31\u53d6\u7b2c2\u4e2a\u548c\u7b2c3\u4e2a\uff0c\u4e5f\u5c31\u662f(lenA + lenB) \/ 2 \u548c (lenA + lenB) \/ 2 + 1<br> &nbsp; &nbsp; &nbsp; &nbsp;int midIndx= len \/ 2, midIndx2 = len \/ 2 + 1;<br> &nbsp; &nbsp; &nbsp; &nbsp;return (getKthElement(A, B, midIndx) + getKthElement(A, B, midIndx2)) \/ 2.0;<br> &nbsp;  }<br>}<br>\u200b<br>\/** \u4e3b\u8981\u601d\u8def\uff1a\u8981\u627e\u5230\u7b2c k(k&gt;1) \u5c0f\u7684\u5143\u7d20\uff0c\u90a3\u4e48\u5c31\u53d6 A[k\/2-1] \u548c B[k\/2-1] \u8fdb\u884c\u6bd4\u8f83<br> * A \u4e2d\u5c0f\u4e8e\u7b49\u4e8e A[k\/2-1] \u7684\u5143\u7d20\u6709 A[0 .. k\/2-2] \u5171\u8ba1 k\/2-1 \u4e2a<br> * B \u4e2d\u5c0f\u4e8e\u7b49\u4e8e B[k\/2-1] \u7684\u5143\u7d20\u6709 B[0 .. k\/2-2] \u5171\u8ba1 k\/2-1 \u4e2a<br> * \u53d6 min(A[k\/2-1], B[k\/2-1])\uff0c\u4e24\u4e2a\u6570\u7ec4\u4e2d\u5c0f\u4e8e\u7b49\u4e8e min(A[k\/2-1], B[k\/2-1]) \u7684\u5143\u7d20\u5171\u8ba1\u4e0d\u4f1a\u8d85\u8fc7 (k\/2-1) + (k\/2-1) = k-2 \u4e2a<br> * \u8fd9\u6837 min(A[k\/2-1], B[k\/2-1]) \u672c\u8eab\u6700\u5927\u4e5f\u53ea\u80fd\u662f\u7b2c k-1 \u5c0f\u7684\u5143\u7d20<br> * \u5982\u679c A[k\/2-1] &lt;= B[k\/2-1]\uff0c\u90a3\u4e48 A[0 ... k\/2-1] \u90fd\u4e0d\u53ef\u80fd\u662f\u7b2c k \u5c0f\u7684\u5143\u7d20\u3002\u628a\u8fd9\u4e9b\u5143\u7d20\u5168\u90e8 \"\u5220\u9664\"\uff0c\u5269\u4e0b\u7684\u4f5c\u4e3a\u65b0\u7684 A \u6570\u7ec4<br> * \u5982\u679c A[k\/2-1] &gt; B[k\/2-1]\uff0c\u90a3\u4e48 B[0 ... k\/2-1] \u90fd\u4e0d\u53ef\u80fd\u662f\u7b2c k \u5c0f\u7684\u5143\u7d20\u3002\u628a\u8fd9\u4e9b\u5143\u7d20\u5168\u90e8 \"\u5220\u9664\"\uff0c\u5269\u4e0b\u7684\u4f5c\u4e3a\u65b0\u7684 B \u6570\u7ec4<br> * \u7531\u4e8e\u6211\u4eec \"\u5220\u9664\" \u4e86\u4e00\u4e9b\u5143\u7d20(\u8fd9\u4e9b\u5143\u7d20\u90fd\u6bd4\u7b2c k \u5c0f\u7684\u5143\u7d20\u8981\u5c0f)\uff0c\u56e0\u6b64\u9700\u8981\u4fee\u6539 k \u7684\u503c\uff0c\u51cf\u53bb\u5220\u9664\u7684\u6570\u7684\u4e2a\u6570<br> * \"\u5220\u9664\"\u662f\u5728indxA\u548cindxB\u4e3a\u57fa\u7ebf\uff0c\u6bd4\u5982A[0 ... k\/2-1]\u5c31\u662fA[indxA ... indxA + k\/2-1],  B[0 ... k\/2-1]\u5c31\u662fB[indxB ... indxB + k\/2-1]\uff0c\u5220\u9664\u65f6\u66f4\u65b0indxA\u548cindxB\u5373\u53ef<br> *\/<br>public int getKthElement(int[] A, int[] B, int k) {<br> &nbsp; &nbsp;int lenA = A.length, lenB = B.length;<br> &nbsp; &nbsp;int indxA = 0, indxB = 0;<br>\u200b<br> &nbsp; &nbsp;while (true) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5f53A\u5df2\u7ecf\u5b8c\u5168\u6392\u9664\u4e86\uff0c\u90a3\u5c31\u76f4\u63a5\u8fd4\u56deB\u4e2d\u7b2ck\u4e2a\u5143\u7d20\u7684\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u8fd9\u4e2a\u7b2ck\u4e2a\u5143\u7d20\u662f\u5728indxB\u7684\u57fa\u7840\u4e4b\u4e0a\u7684\u504f\u79fb\u91cf\uff0c\u5373B[indxB,...,indxB+(k-1),...]<br> &nbsp; &nbsp; &nbsp; &nbsp;if (indxA == lenA ) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return B[indxB + (k - 1)];<br> &nbsp; &nbsp; &nbsp;  }else if (indxB == lenB) {\/\/\u5f53B\u5df2\u7ecf\u5b8c\u5168\u6392\u9664\u4e86\uff0c\u90a3\u5c31\u76f4\u63a5\u8fd4\u56deA\u4e2d\u7b2ck\u4e2a\u5143\u7d20\u7684\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return A[indxA + (k - 1)];<br> &nbsp; &nbsp; &nbsp;  }else if (k == 1) {\/\/\u5982\u679c\u73b0\u5728\u8981\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5373k=1\uff0c\u90a3\u4e48\u5c31\u8fd4\u56deA\u548cB\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e2d\u6700\u5c0f\u7684\u90a3\u4e2a\u5c31\u597d<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return Math.min(A[indxA], B[indxB]);<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5728\u6570\u7ec4A\u4e2d\uff0c\u9009\u62e9\u4eceindxA\u5f00\u59cb\u7b97\u8d77\uff0c\u7b2c k\/2 \u4e2a\u5143\u7d20\u6216\u8005\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\uff0c\u4e5f\u5c31\u662f\u5f53\u524d\u6570\u7ec4A\u4e2d\u8981\u4e0eB\u6bd4\u8f83\u7684\u90a3\u4e2a\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u8fd9\u4e5f\u662f\u4e3a\u4e86\u9632\u6b62\u4e0b\u6807\u8d8a\u754c<br> &nbsp; &nbsp; &nbsp; &nbsp;int tempIndxA = Math.min(indxA + k \/ 2 - 1, lenA - 1);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5728\u6570\u7ec4B\u4e2d\uff0c\u9009\u62e9\u4eceindxB\u5f00\u59cb\u7b97\u8d77\uff0c\u7b2ck \/ 2\u4e2a\u5143\u7d20\u6216\u8005\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\uff0c\u4e5f\u5c31\u662f\u5f53\u524d\u6570\u7ec4B\u4e2d\u8981\u4e0eA\u6bd4\u8f83\u7684\u90a3\u4e2a\u5143\u7d20\u7684\u4e0b\u6807<br> &nbsp; &nbsp; &nbsp; &nbsp;int tempIndxB = Math.min(indxB + k \/ 2 - 1, lenB - 1);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5982\u679cA[tempIndxA] \u5c0f\u4e8e\u7b49\u4e8e B[tempIndxB]\uff0c\u5219\u6392\u9664A[indxA,...,tempIndxA]=tempIndxA - indxA + 1\u4e2a\u5143\u7d20<br> &nbsp; &nbsp; &nbsp; &nbsp;if (A[tempIndxA] &lt;= B[tempIndxB]) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k = k - (tempIndxA - indxA + 1);\/\/\u76ee\u524d\u662f\u8981\u627e\u7b2ck\u4e2a\u5143\u7d20\uff0c\u6392\u9664\u4e86tempIndxA - indxA + 1\u4e2a\uff0c\u6240\u4ee5k\u8981\u51cf\u53bb\u76f8\u5e94\u6570\u91cf<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;indxA = tempIndxA + 1;\/\/indxA\u6307\u5411\u540e\u9762\u4e00\u4e2a\u5143\u7d20\uff0c\u56e0\u4e3a\u524d\u9762A[indxA,...,tempIndxA]\u4e2a\u5143\u7d20\u90fd\u6392\u9664\u4e86<br> &nbsp; &nbsp; &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k = k - (tempIndxB - indxB + 1);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;indxB = tempIndxB + 1;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/pre>\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u5212\u5206\u6570\u7ec4\u89e3\u6cd5<\/h3>\n\n\n<p>\u4e2d\u4f4d\u6570\u7684\u5b9a\u4e49\uff1a\u5c06\u4e00\u4e2a\u96c6\u5408\u5212\u5206\u4e3a\u4e24\u4e2a\u957f\u5ea6\u76f8\u7b49\u7684\u5b50\u96c6\uff0c\u5176\u4e2d\u4e00\u4e2a\u5b50\u96c6\u4e2d\u7684\u5143\u7d20\u603b\u662f\u5927\u4e8e\u53e6\u4e00\u4e2a\u5b50\u96c6\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n\n\n<p>\u9996\u5148\uff0c\u5728\u4efb\u610f\u4f4d\u7f6e i \u5c06A \u5212\u5206\u6210\u4e24\u4e2a\u90e8\u5206\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp; &nbsp; left_A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right_A<\/pre>\n\n\n<pre class=\"wp-block-preformatted\">A[0], A[1], ..., A[i-1] &nbsp;| &nbsp;A[i], A[i+1], ..., A[m-1]<\/pre>\n\n\n<p>\u7531\u4e8e A \u4e2d\u6709 m \u4e2a\u5143\u7d20\uff0c \u6240\u4ee5\u6709 m+1 \u79cd\u5212\u5206\u7684\u65b9\u6cd5<code>(i\u2208[0,m])<\/code>\u3002<\/p>\n\n\n<p>len(left_A)=<em>i<\/em>,len(right_A)=<em>m<\/em>\u2212<em>i<\/em>.<\/p>\n\n\n<p>\u6ce8\u610f\uff1a\u5f53 i = 0\u65f6\uff0cleft_A \u4e3a\u7a7a\u96c6\uff0c \u800c\u5f53 i = m \u65f6, right_A \u4e3a\u7a7a\u96c6\u3002<\/p>\n\n\n<p>\u91c7\u7528\u540c\u6837\u7684\u65b9\u5f0f\uff0c\u5728\u4efb\u610f\u4f4d\u7f6e j \u5c06 B \u5212\u5206\u6210\u4e24\u4e2a\u90e8\u5206\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp; &nbsp; left_B &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right_B<br>B[0], B[1], ..., B[j-1] &nbsp;| &nbsp;B[j], B[j+1], ..., B[n-1]<\/pre>\n\n\n<p>\u5c06left_A \u548c left_B \u653e\u5165\u4e00\u4e2a\u96c6\u5408\uff0c\u5e76\u5c06right_A \u548cright_B\u653e\u5165\u53e6\u4e00\u4e2a\u96c6\u5408\u3002 \u518d\u628a\u8fd9\u4e24\u4e2a\u65b0\u7684\u96c6\u5408\u5206\u522b\u547d\u540d\u4e3a left_part \u548c right_part\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;  left_part &nbsp; &nbsp; &nbsp; &nbsp;  | &nbsp; &nbsp; &nbsp; &nbsp; right_part<\/pre>\n\n\n<pre class=\"wp-block-preformatted\">A[0], A[1], ..., A[i-1]  |  A[i], A[i+1], ..., A[m-1]<\/pre>\n\n\n<pre class=\"wp-block-preformatted\">B[0], B[1], ..., B[j-1]  |  B[j], B[j+1], ..., B[n-1]<\/pre>\n\n\n<p>\u5f53 A \u548c B \u7684\u603b\u957f\u5ea6\u662f\u5076\u6570\u65f6\uff0c\u5982\u679c\u53ef\u4ee5\u786e\u8ba4\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li>len(left_part)=len(right_part)<\/li><li>max(left_part)\u2264min(right_part)<\/li><\/ul>\n\n\n<p>\u90a3\u4e48\uff0c{A,B} \u4e2d\u7684\u6240\u6709\u5143\u7d20\u5df2\u7ecf\u88ab\u5212\u5206\u4e3a\u76f8\u540c\u957f\u5ea6\u7684\u4e24\u4e2a\u90e8\u5206\uff0c\u4e14\u524d\u4e00\u90e8\u5206\u4e2d\u7684\u5143\u7d20\u603b\u662f\u5c0f\u4e8e\u6216\u7b49\u4e8e\u540e\u4e00\u90e8\u5206\u4e2d\u7684\u5143\u7d20\u3002\u4e2d\u4f4d\u6570\u5c31\u662f\u524d\u4e00\u90e8\u5206\u7684\u6700\u5927\u503c\u548c\u540e\u4e00\u90e8\u5206\u7684\u6700\u5c0f\u503c\u7684\u5e73\u5747\u503c\uff1a<\/p>\n\n\n<p>median=(max(left_part)+min(right_part))\/2<\/p>\n\n\n<p>\u5f53 A \u548c B \u7684\u603b\u957f\u5ea6\u662f\u5947\u6570\u65f6\uff0c\u5982\u679c\u53ef\u4ee5\u786e\u8ba4\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li>en(left_part)=len(right_part)+1<\/li><li>max(left_part)\u2264min(right_part)<\/li><\/ul>\n\n\n<p>\u90a3\u4e48\uff0c{A,B} \u4e2d\u7684\u6240\u6709\u5143\u7d20\u5df2\u7ecf\u88ab\u5212\u5206\u4e3a\u4e24\u4e2a\u90e8\u5206\uff0c\u524d\u4e00\u90e8\u5206\u6bd4\u540e\u4e00\u90e8\u5206\u591a\u4e00\u4e2a\u5143\u7d20\uff0c\u4e14\u524d\u4e00\u90e8\u5206\u4e2d\u7684\u5143\u7d20\u603b\u662f\u5c0f\u4e8e\u6216\u7b49\u4e8e\u540e\u4e00\u90e8\u5206\u4e2d\u7684\u5143\u7d20\u3002\u4e2d\u4f4d\u6570\u5c31\u662f\u524d\u4e00\u90e8\u5206\u7684\u6700\u5927\u503c\uff1a<\/p>\n\n\n<p>\u200b<\/p>\n\n\n<p>\u7b2c\u4e00\u4e2a\u6761\u4ef6\u5bf9\u4e8e\u603b\u957f\u5ea6\u662f\u5076\u6570\u548c\u5947\u6570\u7684\u60c5\u51b5\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u662f\u53ef\u4ee5\u5c06\u4e24\u79cd\u60c5\u51b5\u5408\u5e76\u3002\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u5bf9\u4e8e\u603b\u957f\u5ea6\u662f\u5076\u6570\u548c\u5947\u6570\u7684\u60c5\u51b5\u662f\u4e00\u6837\u7684\u3002<\/p>\n\n\n<p>\u8981\u786e\u4fdd\u8fd9\u4e24\u4e2a\u6761\u4ef6\uff0c\u53ea\u9700\u8981\u4fdd\u8bc1\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li><code>i + j = m - i + n - j<\/code>(\u5f53 m+n\u4e3a\u5076\u6570)\u6216<code>i + j = m - i + n - j + 1<\/code>(\u5f53 m+n\u4e3a\u5947\u6570)\u3002\u7b49\u53f7\u5de6\u4fa7\u4e3a\u524d\u4e00\u90e8\u5206\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u7b49\u53f7\u53f3\u4fa7\u4e3a\u540e\u4e00\u90e8\u5206\u7684\u5143\u7d20\u4e2a\u6570\u3002\u5c06 i \u548c j \u5168\u90e8\u79fb\u5230\u7b49\u53f7\u5de6\u4fa7\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230 \u200b\u3002\u8fd9\u91cc\u7684\u5206\u6570\u7ed3\u679c\u53ea\u4fdd\u7559\u6574\u6570\u90e8\u5206\u3002<\/li><li><code>0\u2264i\u2264m\uff0c0\u2264j\u2264n<\/code>\u3002\u5982\u679c\u6211\u4eec\u89c4\u5b9a A \u7684\u957f\u5ea6\u5c0f\u4e8e\u7b49\u4e8e B \u7684\u957f\u5ea6\uff0c\u5373 m\u2264n\u3002\u8fd9\u6837\u5bf9\u4e8e\u4efb\u610f\u7684<code>i\u2208[0,m]<\/code>\uff0c\u90fd\u6709\u200b\u2208[0,n]\uff0c\u90a3\u4e48\u6211\u4eec\u5728 [0,m] \u7684\u8303\u56f4\u5185\u679a\u4e3e i \u5e76\u5f97\u5230 j \uff0c\u5c31\u4e0d\u9700\u8981\u989d\u5916\u7684\u6027\u8d28\u4e86\u3002<ul><li>\u5982\u679cA \u7684\u957f\u5ea6\u8f83\u5927\uff0c\u90a3\u4e48\u6211\u4eec\u53ea\u8981\u4ea4\u6362A \u548c B \u5373\u53ef\u3002<\/li><li>\u5982\u679c m &gt; n \uff0c\u90a3\u4e48\u5f97\u51fa\u7684 j\u6709\u53ef\u80fd\u662f\u8d1f\u6570\u3002<\/li><\/ul><\/li><li><code>B[j\u22121]\u2264A[i]<\/code>\u4ee5\u53ca<code>A[i\u22121]\u2264B[j]<\/code>\uff0c\u5373\u524d\u4e00\u90e8\u5206\u7684\u6700\u5927\u503c\u5c0f\u4e8e\u7b49\u4e8e\u540e\u4e00\u90e8\u5206\u7684\u6700\u5c0f\u503c\u3002<\/li><\/ul>\n\n\n<p>\u4e3a\u4e86\u7b80\u5316\u5206\u6790\uff0c\u5047\u8bbe<code>A[i\u22121],B[j\u22121],A[i],B[j]<\/code>\u603b\u662f\u5b58\u5728\u3002\u5bf9\u4e8e<code>i=0\u3001i=m\u3001j=0\u3001j=n<\/code>\u8fd9\u6837\u7684\u4e34\u754c\u6761\u4ef6\uff0c\u6211\u4eec\u53ea\u9700\u8981\u89c4\u5b9a <code>A[\u22121]=B[\u22121]=\u2212\u221e\uff0cA[m]=B[n]=\u221e<\/code>\u5373\u53ef\u3002\u8fd9\u4e5f\u662f\u6bd4\u8f83\u76f4\u89c2\u7684\uff1a\u5f53\u4e00\u4e2a\u6570\u7ec4\u4e0d\u51fa\u73b0\u5728\u524d\u4e00\u90e8\u5206\u65f6\uff0c\u5bf9\u5e94\u7684\u503c\u4e3a\u8d1f\u65e0\u7a77\uff0c\u5c31\u4e0d\u4f1a\u5bf9\u524d\u4e00\u90e8\u5206\u7684\u6700\u5927\u503c\u4ea7\u751f\u5f71\u54cd\uff1b\u5f53\u4e00\u4e2a\u6570\u7ec4\u4e0d\u51fa\u73b0\u5728\u540e\u4e00\u90e8\u5206\u65f6\uff0c\u5bf9\u5e94\u7684\u503c\u4e3a\u6b63\u65e0\u7a77\uff0c\u5c31\u4e0d\u4f1a\u5bf9\u540e\u4e00\u90e8\u5206\u7684\u6700\u5c0f\u503c\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u9700\u8981\u505a\u7684\u662f\uff1a<\/p>\n\n\n<p>\u5728 [0, m]\u4e2d\u627e\u5230 i\uff0c\u4f7f\u5f97\uff1a<\/p>\n\n\n<p>B[j\u22121]\u2264A[i] \u4e14 A[i\u22121]\u2264B[j]\uff0c\u5176\u4e2d \u200bj = (m + n + 1)\/2 &#8211; i<\/p>\n\n\n<p>\u6211\u4eec\u8bc1\u660e\u5b83\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n<p>\u5728 [0, m]\u4e2d\u627e\u5230\u6700\u5927\u7684 i\uff0c\u4f7f\u5f97\uff1a<\/p>\n\n\n<p>A[i\u22121]\u2264B[j]\uff0c\u5176\u4e2d\u200b\u200b\u200bj = (m + n + 1)\/2 &#8211; i<\/p>\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li>\u5f53 i\u4ece0\u223cm \u9012\u589e\u65f6\uff0cA[i\u22121] \u9012\u589e\uff0cB[j] \u9012\u51cf\uff0c\u6240\u4ee5\u4e00\u5b9a\u5b58\u5728\u4e00\u4e2a\u6700\u5927\u7684 i\u6ee1\u8db3<code>A[i\u22121]\u2264B[j]<\/code>\uff1b<\/li><li>\u5982\u679c i\u662f\u6700\u5927\u7684\uff0c\u90a3\u4e48\u8bf4\u660e i+1 \u4e0d\u6ee1\u8db3\u3002\u5c06 i+1\u5e26\u5165\u53ef\u4ee5\u5f97\u5230 <code>A[i]&gt;B[j\u22121]<\/code>\uff0c\u4e5f\u5c31\u662f<code>B[j\u22121]&lt;A[i]<\/code>\uff0c\u5c31\u548c\u6211\u4eec\u8fdb\u884c\u7b49\u4ef7\u53d8\u6362\u524d i \u7684\u6027\u8d28\u4e00\u81f4\u4e86<\/li><\/ul>\n\n\n<p>\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5bf9 i \u5728 [0, m] \u7684\u533a\u95f4\u4e0a\u8fdb\u884c\u4e8c\u5206\u641c\u7d22\uff0c\u627e\u5230\u6700\u5927\u7684\u6ee1\u8db3 <code>A[i\u22121]\u2264B[j]<\/code> \u7684 i \u503c\uff0c\u5c31\u5f97\u5230\u4e86\u5212\u5206\u7684\u65b9\u6cd5\u3002\u6b64\u65f6\uff0c\u5212\u5206\u524d\u4e00\u90e8\u5206\u5143\u7d20\u4e2d\u7684\u6700\u5927\u503c\uff0c\u4ee5\u53ca\u5212\u5206\u540e\u4e00\u90e8\u5206\u5143\u7d20\u4e2d\u7684\u6700\u5c0f\u503c\uff0c\u624d\u53ef\u80fd\u4f5c\u4e3a\u5c31\u662f\u8fd9\u4e24\u4e2a\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\u3002<\/p>\n\n\n<p>\u6ce8\uff1a\u53d6\u81eaLeeCode\u5b98\u65b9\u89e3\u9898\u601d\u8def<\/p>\n\n\n<pre class=\"wp-block-preformatted\">\u200b<br>\/\/4.\u5212\u5206\u6570\u7ec4<br>\/\/\u65f6\u95f4\u590d\u6742\u5ea6: O(logmin(m,n))), \u7a7a\u95f4\u590d\u6742\u5ea6: O(1)<br>public double findMedianSortedArrays(int[] A, int[] B) {<br> &nbsp; &nbsp;\/\/\u4fdd\u8bc1lenA &lt; lenB<br> &nbsp; &nbsp;if (A.length &gt; B.length) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return findMedianSortedArrays(B, A);<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;int m = A.length;<br> &nbsp; &nbsp;int n = B.length;<br> &nbsp; &nbsp;int left = 0, right = m;<br> &nbsp; &nbsp;\/\/ medianFront\uff1a\u524d\u4e00\u90e8\u5206\u7684\u6700\u5927\u503c<br> &nbsp; &nbsp;\/\/ medianBack\uff1a\u540e\u4e00\u90e8\u5206\u7684\u6700\u5c0f\u503c<br> &nbsp; &nbsp;int medianFront = 0, medianBack = 0;<br>\u200b<br> &nbsp; &nbsp;while (left &lt;= right) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u524d\u4e00\u90e8\u5206\u5305\u542b A[0 .. i-1] \u548c B[0 .. j-1]<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u540e\u4e00\u90e8\u5206\u5305\u542b A[i .. m-1] \u548c B[j .. n-1]<br> &nbsp; &nbsp; &nbsp; &nbsp;int i = (left + right) \/ 2;<br> &nbsp; &nbsp; &nbsp; &nbsp;int j = (m + n + 1) \/ 2 - i;<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/A_i_1 \u8868\u793a A[i-1]<br> &nbsp; &nbsp; &nbsp; &nbsp;int A_i_1 = (i == 0 ? Integer.MIN_VALUE : A[i - 1]);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/A_i \u8868\u793a A[i]<br> &nbsp; &nbsp; &nbsp; &nbsp;int A_i = (i == m ? Integer.MAX_VALUE : A[i]);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/B_j_1 \u8868\u793a B[j-1]<br> &nbsp; &nbsp; &nbsp; &nbsp;int B_j_1 = (j == 0 ? Integer.MIN_VALUE : B[j - 1]);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/B_j \u8868\u793a B[j]<br> &nbsp; &nbsp; &nbsp; &nbsp;int B_j = (j == n ? Integer.MAX_VALUE : B[j]);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/A[i-1] &lt;= B[j]<br> &nbsp; &nbsp; &nbsp; &nbsp;if (A_i_1 &lt;= B_j) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;medianFront = Math.max(A_i_1, B_j_1);\/\/\u53d6\u524d\u534a\u90e8\u5206\u7684\u6700\u5927\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;medianBack = Math.min(A_i, B_j);\/\/\u53d6\u540e\u534a\u90e8\u5206\u7684\u6700\u5c0f\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = i + 1;\/\/\u5254\u9664A[left,...,i]\u7684\u90e8\u5206\uff0ci\u53bb\u53d6A[left+1,...,right]<br> &nbsp; &nbsp; &nbsp;  }else{<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = i - 1;\/\/\u5254\u9664A[i,...,right]\u7684\u90e8\u5206\uff0ci\u53bb\u53d6A[left,...,i-1]<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;return (m + n) % 2 == 0 ? (medianFront + medianBack) \/ 2.0 : medianFront;<br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e24\u4e2a\u5927\u5c0f\u5206\u522b\u4e3am\u548cn\u7684\u6b63\u5e8f(\u4ece\u5c0f\u5230\u5927)\u6570\u7ec4nums1\u548cnums2\u3002\u8bf7\u4f60\u627e\u51fa\u5e76\u8fd4\u56de\u8fd9\u4e24\u4e2a\u6b63\u5e8f\u6570\u7ec4\u7684\u4e2d\u4f4d\u6570\u3002  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-leecode"],"_links":{"self":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=186"}],"version-history":[{"count":0,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}