<?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>backtracking Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/backtracking/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/backtracking/</link>
	<description></description>
	<lastBuildDate>Thu, 19 Apr 2018 15:39:46 +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>backtracking Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/backtracking/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 216. Combination Sum III</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-216-combination-sum-iii/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-216-combination-sum-iii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 04 Nov 2017 03:48:26 +0000</pubDate>
				<category><![CDATA[Medium]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[backtracking]]></category>
		<category><![CDATA[combination]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[sum]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=724</guid>

					<description><![CDATA[<p>题目大意：输出所有用k个数的和为n的组合。可以使用的元素是1到9。 Problem: Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-216-combination-sum-iii/">花花酱 LeetCode 216. Combination Sum III</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div>
<p><iframe width="500" height="375" src="https://www.youtube.com/embed/UwdX19UvoCI?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：输出所有用k个数的和为n的组合。可以使用的元素是1到9。</p>
<p><strong>Problem:</strong></p>
<p>Find all possible combinations of <i><b>k</b></i> numbers that add up to a number <i><b>n</b></i>, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.</p>
</div>
<div>
<p><i><b>Example 1:</b></i></p>
<p>Input: <i><b>k</b></i> = 3, <i><b>n</b></i> = 7</p>
<p>Output:</p><pre class="crayon-plain-tag">[[1,2,4]]</pre><p><i><b>Example 2:</b></i></p>
<p>Input: <i><b>k</b></i> = 3, <i><b>n</b></i> = 9</p>
<p>Output:</p><pre class="crayon-plain-tag">[[1,2,6], [1,3,5], [2,3,4]]</pre><p>
</p></div>
<p>&nbsp;</p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><strong>Idea:</strong></p>
<p>DFS + backtracking</p>
<p>bit</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/216-ep100.png"><img class="alignnone size-full wp-image-736" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/216-ep100.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/216-ep100.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/216-ep100-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/216-ep100-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/11/216-ep100-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 0 ms
class Solution {
public:
    vector&lt;vector&lt;int&gt;&gt; combinationSum3(int k, int n) {
        vector&lt;vector&lt;int&gt;&gt; ans;
        vector&lt;int&gt; cur;
        dfs(k, n, 1, cur, ans);
        return ans;
    }
private:
    // Use k numbers (&gt;= s) to sum up to n
    void dfs(int k, int n, int s, 
             vector&lt;int&gt;&amp; cur, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {
        if (k == 0) {
            if (n == 0) ans.push_back(cur);
            return;
        }
        
        for (int i = s; i &lt;= 9; ++i) {
            if (i &gt; n) return;
            cur.push_back(i);
            dfs(k - 1, n - i, i + 1, cur, ans);
            cur.pop_back();
        }
    }
};</pre><p>&nbsp;</p>
<p>C++ / binary</p><pre class="crayon-plain-tag">class Solution {
public:
    vector&lt;vector&lt;int&gt;&gt; combinationSum3(int k, int n) {
        vector&lt;vector&lt;int&gt;&gt; ans;
        
        // 2^9, generate all combinations of [1 .. 9]
        for (int i = 0; i &lt; (1 &lt;&lt; 9); ++i) {
            vector&lt;int&gt; cur;
            int sum = 0;
            // Use j if (j - 1)-th bit is 1
            for (int j = 1; j &lt;= 9; ++j)
                if (i &amp; (1 &lt;&lt; (j - 1))) {
                    sum += j;
                    cur.push_back(j);
                }
            if (sum == n &amp;&amp; cur.size() == k) 
                ans.push_back(cur);
        }
        
        return ans;
    }
};</pre><p>&nbsp;</p>
<p>Python</p><pre class="crayon-plain-tag">class Solution:
    def combinationSum3(self, k, n):
        def dfs(k, n, s, cur, ans):
            if k == 0 and n == 0: 
                ans.append(list(cur))
            for i in range(s, min(n + 1, 10)):
                dfs(k - 1, n - i, i + 1, cur + [i], ans)
        
        ans = []        
        dfs(k, n, 1, [], ans)
        return ans</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>Related problems:</strong></p>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-39-combination-sum/">[解题报告] LeetCode 39. Combination Sum</a></li>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-40-combination-sum-ii/">[解题报告] LeetCode 40. Combination Sum II</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-216-combination-sum-iii/">花花酱 LeetCode 216. Combination Sum III</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/searching/leetcode-216-combination-sum-iii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
