{"id":157587,"date":"2020-09-08T08:02:30","date_gmt":"2020-09-08T00:02:30","guid":{"rendered":"http:\/\/4563.org\/?p=157587"},"modified":"2020-09-08T08:02:30","modified_gmt":"2020-09-08T00:02:30","slug":"%e4%b8%a4%e6%95%b0%e4%b9%8b%e5%92%8c-iii-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e8%ae%be%e8%ae%a1","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=157587","title":{"rendered":"\u4e24\u6570\u4e4b\u548c III-\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e24\u6570\u4e4b\u548c III-\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a TwoSum \u7c7b\u3002\u4ed6\u9700\u8981\u652f\u6301\u4ee5\u4e0b\u64cd\u4f5c:add \u548c find \u3002<\/p>\n<p>add -\u628a\u8fd9\u4e2a\u6570\u6dfb\u52a0\u5230\u5185\u90e8\u7684\u6570\u636e\u7ed3\u6784\u3002 find -\u662f\u5426\u5b58\u5728\u4efb\u610f\u4e00\u5bf9\u6570\u5b57\u4e4b\u548c\u7b49\u4e8e\u8fd9\u4e2a\u503c<\/p>\n<p>\u70b9\u6b64\u5728\u7ebf\u505a\u9898<\/p>\n<h2>\u6837\u4f8b 1:<\/h2>\n<pre><code>add(1);add(3);add(5); find(4)\/\/\u8fd4\u56de true find(7)\/\/\u8fd4\u56de false <\/code><\/pre>\n<h2>[\u9898\u89e3]<\/h2>\n<p>\u4f7f\u7528\u54c8\u5e0c\u8868\u7684\u65b9\u6cd5\u662f\u6700\u5feb\u7684\u3002<\/p>\n<ul>\n<li>add \u53ef\u4ee5\u505a\u5230 O(1)<\/li>\n<li>find \u53ef\u4ee5\u505a\u5230 O(n)<\/li>\n<\/ul>\n<pre><code>public class TwoSum {     private Map&lt;Integer, Integer&gt; counter;          public TwoSum() {         counter = new HashMap&lt;Integer, Integer&gt;();     }      \/\/ Add the number to an internal data structure.     public void add(int number) {         counter.put(number, counter.getOrDefault(number, 0) + 1);     }      \/\/ Find if there exists any pair of numbers which sum is equal to the value.     public boolean find(int value) {         for (Integer num1 : counter.keySet()) {             int num2 = value - num1;             int desiredCount = num1 == num2 ? 2 : 1;             if (counter.getOrDefault(num2, 0) &gt;= desiredCount) {                 return true;             }         }         return false;     } } \/\/ Your TwoSum object will be instantiated and called as such:\/\/ TwoSum twoSum = new TwoSum();\/\/ twoSum.add(number);\/\/ twoSum.find(value);  \u6392\u5e8f\u6570\u7ec4+\u53cc\u6307\u9488\u7684\u7b97\u6cd5\u4f1a\u8d85\u65f6\uff0c\u4f46\u662f\u5927\u5bb6\u53ef\u4ee5\u770b\u770b\u662f\u600e\u4e48\u5199\u7684\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\uff1a - add O(n) - find O(n)  public class TwoSum {     public List&lt;Integer&gt; nums;     public TwoSum() {         nums = new ArrayList&lt;Integer&gt;();     }          public void add(int number) {         nums.add(number);         int index = nums.size() - 1;         while (index &gt; 0 &amp;&amp; nums.get(index - 1) &gt; nums.get(index)) {             int temp = nums.get(index);             nums.set(index, nums.get(index - 1));             nums.set(index - 1, temp);             index--;         }     }      public boolean find(int value) {         int left = 0, right = nums.size() - 1;         while (left &lt; right) {             int twoSum = nums.get(left) + nums.get(right);             if (twoSum &lt; value) {                 left++;             } else if (twoSum &gt; value) {                 right--;             } else {                 return true;             }         }         return false;     } } <\/code><\/pre>\n<p>\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>\u4e24\u6570\u4e4b\u548c III-\u6570\u636e\u7ed3\u6784\u8bbe\u8ba1 \u8cc7&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\/157587"}],"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=157587"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/157587\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157587"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}