{"id":324789,"date":"2021-02-06T13:08:33","date_gmt":"2021-02-06T05:08:33","guid":{"rendered":"http:\/\/4563.org\/?p=324789"},"modified":"2021-02-06T13:08:33","modified_gmt":"2021-02-06T05:08:33","slug":"%e5%ad%97%e8%8a%82%e8%bf%99%e9%81%93%e9%9d%a2%e8%af%95%e5%8e%9f%e9%a2%98%e9%99%a4%e4%ba%86%e7%94%a8%e4%ba%8c%e5%88%86%e6%9f%a5%e6%89%be%ef%bc%8c%e6%9a%b4%e5%8a%9b%e6%9e%9a%e4%b8%be%ef%bc%8c%e8%bf%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=324789","title":{"rendered":"\u5b57\u8282\u8fd9\u9053\u9762\u8bd5\u539f\u9898\u9664\u4e86\u7528\u4e8c\u5206\u67e5\u627e\uff0c\u66b4\u529b\u679a\u4e3e\uff0c\u8fd8\u6709\u522b\u7684\u65b9\u6cd5\u5417\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5b57\u8282\u8fd9\u9053\u9762\u8bd5\u539f\u9898\u9664\u4e86\u7528\u4e8c\u5206\u67e5\u627e\uff0c\u66b4\u529b\u679a\u4e3e\uff0c\u8fd8\u6709\u522b\u7684\u65b9\u6cd5\u5417\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/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<h3>\u63cf\u8ff0<\/h3>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff0c\u627e\u51fa\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u5bf9\u7684\u548c\u662f\u5c0f\u4e8e\u6216\u7b49\u4e8e\u76ee\u6807\u503c\u3002\u8fd4\u56de\u5bf9\u6570\u3002<\/p>\n<p>\u9898\u76ee\u539f\u5730\u5740<\/p>\n<h3>\u6837\u4f8b 1<\/h3>\n<pre><code>\u8f93\u5165: nums = [2, 7, 11, 15], target = 24.  \u8f93\u51fa: 5.  \u89e3\u91ca: 2 + 7 &lt; 24 2 + 11 &lt; 24 2 + 15 &lt; 24 7 + 11 &lt; 24 7 + 15 &lt; 24 <\/code><\/pre>\n<h3>\u6837\u4f8b 2<\/h3>\n<pre><code>\u8f93\u5165: nums = [1], target = 1.  \u8f93\u51fa: 0.  <\/code><\/pre>\n<h2>\u7b97\u6cd5<\/h2>\n<h3>\u7b97\u6cd5\u4e00 \u3001\u66b4\u529b\u679a\u4e3e<\/h3>\n<p>\u66b4\u529b N2N2 \u679a\u4e3e<\/p>\n<h3>\u7b97\u6cd5\u4e8c \u3001\u4e8c\u5206\u67e5\u627e<\/h3>\n<h3>\u7b97\u6cd5\u601d\u8def<\/h3>\n<p>\u7b97\u6cd5\u4e8c\u5728\u7b97\u6cd5\u4e00\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u6539\u8fdb<\/p>\n<p>\u679a\u4e3e\u4e00\u4e2a\u6570 nums[i]nums[i],\u627e\u6709\u591a\u5c11\u4e2a j nums[i]+nums[j]&lt;=targetnums[i]+nums[j]&lt;=target \u53ef\u4ee5\u5148\u5bf9 numsnums \u6392\u5e8f \u7136\u540e\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7684\u65b9\u5f0f\u5feb\u901f\u67e5\u627e<\/p>\n<p>\u590d\u6742\u5ea6\u5206\u6790<\/p>\n<ul>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6<\/li>\n<li>\u4e09\u4e2a\u7b97\u6cd5\u90fd\u4e0d\u9700\u8981\u591a\u5f00\u8f9f\u7a7a\u95f4\uff0c\u56e0\u6b64\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\u3002<\/li>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6<\/li>\n<li>\u7b97\u6cd5\u4e00 \u66b4\u529b\u679a\u4e3e O(N2)O(N2)<\/li>\n<li>\u7b97\u6cd5\u4e8c \u679a\u4e3e ii\uff0c\u7528\u4e8c\u5206\u67e5\u627e\u52a0\u901f jj \u7684\u6570\u91cf \u65f6\u95f4\u590d\u6742\u5ea6 O(NlogN)O(NlogN)<\/li>\n<\/ul>\n<h3>\u4ee3\u7801\uff1a\u7b97\u6cd5\u4e8c<\/h3>\n<pre><code>class Solution {   public:     \/**      * @param nums: an array of integer      * @param target: an integer      * @return: an integer      *\/     int twoSum5(vector&lt;int&gt; &amp;nums, int target) {         \/\/ write your code here          \/\/\u5148\u5bf9\u6570\u7ec4\u6392\u5e8f         sort(nums.begin(), nums.end());         \/\/\u6570\u7ec4\u957f\u5ea6         int n = nums.size();          int ans = 0;         \/\/\u5bf9\u4e8e\u6bcf\u4e2a i\uff0c\u4e8c\u5206\u627e\u5230\u6700\u5927\u7684 j nums[i]+nums[j]&lt;=target         for(int i = 0; i &lt; n - 1; i++) {             \/\/\u786e\u5b9a\u4e8c\u5206\u4e0a\u4e0b\u754c             int left = i;             int right = n;             int pos = i;             while(left + 1 &lt; right) {                 int mid = left + (right - left) \/ 2;                 \/\/\u4e0d\u5927\u4e8e target \u53ef\u4ee5\u63d0\u9ad8\u4e0b\u754c                 if(nums[mid] + nums[i] &lt;= target) {                     left = mid;                     pos = mid;                 }                 \/\/\u7f29\u5c0f\u4e0a\u754c                 else {                     right = mid;                 }             }             \/\/\u7edf\u8ba1\u7b54\u6848             ans += pos - i;         }         return ans;     } }  <\/code><\/pre>\n<p>\u53ef\u4ee5\u5728\u8fd9\u91cc\u67e5\u770b\u9898\u76ee\u539f\u5730\u5740<\/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>\u5b57\u8282\u8fd9\u9053\u9762\u8bd5\u539f\u9898\u9664\u4e86\u7528\u4e8c\u5206\u67e5\u627e\uff0c\u66b4&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\/324789"}],"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=324789"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/324789\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324789"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}