<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nsums Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/nsums/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/nsums/</link>
	<description></description>
	<lastBuildDate>Mon, 15 Feb 2021 20:53:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>nsums Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/nsums/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 15. 3Sum</title>
		<link>https://zxi.mytechroad.com/blog/two-pointers/leetcode-15-3sum/</link>
					<comments>https://zxi.mytechroad.com/blog/two-pointers/leetcode-15-3sum/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 07 Mar 2018 06:16:09 +0000</pubDate>
				<category><![CDATA[Two pointers]]></category>
		<category><![CDATA[3sum]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[nsums]]></category>
		<category><![CDATA[two pointers]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2005</guid>

					<description><![CDATA[<p>题目大意：给你一个数组，让你找出3个数的和为0的所有组合。 Problem: https://leetcode.com/problems/3sum/description/ Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-15-3sum/">花花酱 LeetCode 15. 3Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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">
<iframe title="花花酱 LeetCode 15. 3Sum - 刷题找工作 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" allowfullscreen></iframe>
</div></figure>


<p>题目大意：给你一个数组，让你找出3个数的和为0的所有组合。</p>
<p><strong>Problem:</strong></p>
<p><a href="https://leetcode.com/problems/3sum/description/">https://leetcode.com/problems/3sum/description/</a></p>
<p>Given an array <i>S</i> of <i>n</i> integers, are there elements <i>a</i>, <i>b</i>, <i>c</i> in <i>S</i> such that <i>a</i> + <i>b</i> + <i>c</i> = 0? Find all unique triplets in the array which gives the sum of zero.</p>
<p><b>Note:</b> The solution set must not contain duplicate triplets.</p>
<pre class="crayon-plain-tag">For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]</pre>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p><strong>Solution 1: Hashtable</strong></p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p>Time complexity: O(n^2)<br />Space complexity: O(n)</p>
<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) { 
    sort(begin(nums), end(nums));
    const int n = nums.size();
    unordered_map&lt;int, int&gt; c;
    for (int x : nums) ++c[x];
    vector&lt;vector&lt;int&gt;&gt; ans;
    for (int i = 0; i &lt; n; ++i) {
      if (i &amp;&amp; nums[i] == nums[i - 1]) continue;
      for (int j = i + 1; j &lt; n; ++j) {
        if (j - 1 != i &amp;&amp; nums[j] == nums[j - 1]) continue;
        const int t = 0 - nums[i] - nums[j];
        // nums[i] &lt;= nums[j] &lt;= nums[k]
        if (t &lt; nums[j]) continue; 
        if (!c.count(t)) continue;
        // Make sure we have enough count
        if (c[t] &gt;= 1 + (nums[i] == t) + (nums[j] == t))
          ans.push_back({nums[i], nums[j], t});        
      }      
    }
    return ans;
  }
};</pre>
<p><strong>Solution 2: Sorting + Two pointers</strong></p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<p>Time complexity: O(nlogn + n^2)</p>
<p>Space complexity: O(1)</p>
<p>C++</p>
<pre class="crayon-plain-tag">class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; threeSum(vector&lt;int&gt;&amp; nums) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    std::sort(nums.begin(), nums.end());
    const int n = nums.size();
    for (int i = 0; i &lt; n - 2; ++i) {
      if (nums[i] &gt; 0) break;
      if (i &gt; 0 &amp;&amp; nums[i] == nums[i - 1]) continue;
      int l = i + 1;
      int r = n - 1;      
      while (l &lt; r) {
        if (nums[i] + nums[l] + nums[r] == 0) {
          ans.push_back({nums[i], nums[l++], nums[r--]});
          while (l &lt; r &amp;&amp; nums[l] == nums[l - 1]) ++l;
          while (l &lt; r &amp;&amp; nums[r] == nums[r + 1]) --r;          
        } else if (nums[i] + nums[l] + nums[r] &lt; 0) {
          ++l;
        } else {
          --r;
        }
      }
    }
    return ans;
  }
};</pre>
<p><strong>Related Problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/hashtable/leetcode-454-4sum-ii/">花花酱 LeetCode 454. 4Sum II</a></li>
<li><a href="http://zxi.mytechroad.com/blog/hashtable/leetcode-1-two-sum/">[解题报告] LeetCode 1. Two Sum 花花酱</a></li>
</ul><p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/two-pointers/leetcode-15-3sum/">花花酱 LeetCode 15. 3Sum</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/two-pointers/leetcode-15-3sum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
