{"id":2005,"date":"2018-03-06T22:16:09","date_gmt":"2018-03-07T06:16:09","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2005"},"modified":"2021-02-15T12:53:42","modified_gmt":"2021-02-15T20:53:42","slug":"leetcode-15-3sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-15-3sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 15. 3Sum"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 15. 3Sum - \u5237\u9898\u627e\u5de5\u4f5c EP383\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/zQDbMjNsuvY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u8ba9\u4f60\u627e\u51fa3\u4e2a\u6570\u7684\u548c\u4e3a0\u7684\u6240\u6709\u7ec4\u5408\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/3sum\/description\/\">https:\/\/leetcode.com\/problems\/3sum\/description\/<\/a><\/p>\n<p>Given an array\u00a0<i>S<\/i>\u00a0of\u00a0<i>n<\/i>\u00a0integers, are there elements\u00a0<i>a<\/i>,\u00a0<i>b<\/i>,\u00a0<i>c<\/i>\u00a0in\u00a0<i>S<\/i>\u00a0such that\u00a0<i>a<\/i>\u00a0+\u00a0<i>b<\/i>\u00a0+\u00a0<i>c<\/i>\u00a0= 0? Find all unique triplets in the array which gives the sum of zero.<\/p>\n<p><b>Note:<\/b>\u00a0The solution set must not contain duplicate triplets.<\/p>\n<pre class=\"\">For example, given array S = [-1, 0, 1, 2, -1, -4],\n\nA solution set is:\n[\n  [-1, 0, 1],\n  [-1, -1, 2]\n]<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8115\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><strong>Solution 1: Hashtable<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8116\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>Time complexity: O(n^2)<br \/>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) { \n    sort(begin(nums), end(nums));\n    const int n = nums.size();\n    unordered_map&lt;int, int&gt; c;\n    for (int x : nums) ++c[x];\n    vector&lt;vector&lt;int&gt;&gt; ans;\n    for (int i = 0; i &lt; n; ++i) {\n      if (i &amp;&amp; nums[i] == nums[i - 1]) continue;\n      for (int j = i + 1; j &lt; n; ++j) {\n        if (j - 1 != i &amp;&amp; nums[j] == nums[j - 1]) continue;\n        const int t = 0 - nums[i] - nums[j];\n        \/\/ nums[i] &lt;= nums[j] &lt;= nums[k]\n        if (t &lt; nums[j]) continue; \n        if (!c.count(t)) continue;\n        \/\/ Make sure we have enough count\n        if (c[t] &gt;= 1 + (nums[i] == t) + (nums[j] == t))\n          ans.push_back({nums[i], nums[j], t});        \n      }      \n    }\n    return ans;\n  }\n};\n\n<\/pre>\n<p><strong>Solution 2: Sorting + Two pointers<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-8117\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-3.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-8118\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-4.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-4.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-4-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/03\/15-ep383-4-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p>Time complexity: O(nlogn + n^2)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true  \">class Solution {\npublic:\n  vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) {\n    vector&lt;vector&lt;int&gt;&gt; ans;\n    std::sort(nums.begin(), nums.end());\n    const int n = nums.size();\n    for (int i = 0; i &lt; n - 2; ++i) {\n      if (nums[i] &gt; 0) break;\n      if (i &gt; 0 &amp;&amp; nums[i] == nums[i - 1]) continue;\n      int l = i + 1;\n      int r = n - 1;      \n      while (l &lt; r) {\n        if (nums[i] + nums[l] + nums[r] == 0) {\n          ans.push_back({nums[i], nums[l++], nums[r--]});\n          while (l &lt; r &amp;&amp; nums[l] == nums[l - 1]) ++l;\n          while (l &lt; r &amp;&amp; nums[r] == nums[r + 1]) --r;          \n        } else if (nums[i] + nums[l] + nums[r] &lt; 0) {\n          ++l;\n        } else {\n          --r;\n        }\n      }\n    }\n    return ans;\n  }\n};<\/pre>\n<p><strong>Related Problems:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-454-4sum-ii\/\">\u82b1\u82b1\u9171 LeetCode 454. 4Sum II<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1-two-sum\/\">[\u89e3\u9898\u62a5\u544a] LeetCode 1. Two Sum \u82b1\u82b1\u9171<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\uff0c\u8ba9\u4f60\u627e\u51fa3\u4e2a\u6570\u7684\u548c\u4e3a0\u7684\u6240\u6709\u7ec4\u5408\u3002 Problem: https:\/\/leetcode.com\/problems\/3sum\/description\/ Given an array\u00a0S\u00a0of\u00a0n\u00a0integers, are there elements\u00a0a,\u00a0b,\u00a0c\u00a0in\u00a0S\u00a0such that\u00a0a\u00a0+\u00a0b\u00a0+\u00a0c\u00a0= 0? Find all unique triplets in the array which gives the sum of zero. Note:\u00a0The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[237,177,236,175],"class_list":["post-2005","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-3sum","tag-medium","tag-nsums","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2005"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2005\/revisions"}],"predecessor-version":[{"id":8119,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2005\/revisions\/8119"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}