{"id":531,"date":"2017-10-03T22:50:40","date_gmt":"2017-10-04T05:50:40","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=531"},"modified":"2018-04-19T08:34:45","modified_gmt":"2018-04-19T15:34:45","slug":"leetcode-39-combination-sum","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-39-combination-sum\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 39. Combination Sum"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 39. Combination Sum  - \u5237\u9898\u627e\u5de5\u4f5c EP81\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/zIY2BWdsbFs?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><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a\u00a0<b>set<\/b>\u00a0of candidate numbers (<b><i>C<\/i><\/b>)\u00a0<b>(without duplicates)<\/b>\u00a0and a target number (<b><i>T<\/i><\/b>), find all unique combinations in\u00a0<b><i>C<\/i><\/b>\u00a0where the candidate numbers sums to\u00a0<b><i>T<\/i><\/b>.<\/p>\n<p>The\u00a0<b>same<\/b>\u00a0repeated number may be chosen from\u00a0<b><i>C<\/i><\/b>\u00a0unlimited number of times.<\/p>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li>All numbers (including target) will be positive integers.<\/li>\n<li>The solution set must not contain duplicate combinations.<\/li>\n<\/ul>\n<p>For example, given candidate set\u00a0<code>[2, 3, 6, 7]<\/code>\u00a0and target\u00a0<code>7<\/code>,<br \/>\nA solution set is:<\/p>\n<pre class=\"\">[\r\n  [7],\r\n  [2, 2, 3]\r\n]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p>DFS<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-534\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-532\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/39-ep81-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution: <\/strong>C++<\/p>\n<pre class=\"lang:c++ decode:true\">class Solution {\r\npublic:\r\n    vector&lt;vector&lt;int&gt;&gt; combinationSum(vector&lt;int&gt;&amp; candidates, int target) {\r\n        vector&lt;vector&lt;int&gt;&gt; ans;\r\n        vector&lt;int&gt; cur;\r\n        std::sort(candidates.begin(), candidates.end());\r\n        dfs(candidates, target, 0, cur, ans);\r\n        return ans;\r\n    }\r\nprivate:\r\n    void dfs(vector&lt;int&gt;&amp; candidates, int target, int s, vector&lt;int&gt;&amp; cur, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {\r\n        if (target == 0) {\r\n            ans.push_back(cur);\r\n            return;\r\n        }\r\n        \r\n        for (int i = s; i &lt; candidates.size(); ++i) {\r\n            if (candidates[i] &gt; target) break;\r\n            cur.push_back(candidates[i]);\r\n            dfs(candidates, target - candidates[i], i, cur, ans);\r\n            cur.pop_back();\r\n        }\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>Python<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRuntime: 75 ms (batter than 97.04%)\r\n\"\"\"\r\nclass Solution(object):\r\n    def combinationSum(self, candidates, target):\r\n        def dfs(candidates, target, s, curr, ans):\r\n            if target == 0: \r\n                ans.append(curr[:])\r\n                return\r\n            \r\n            for i in xrange(s, len(candidates)):\r\n                if candidates[i] &gt; target: return\r\n                curr.append(candidates[i])\r\n                dfs(candidates, target - candidates[i], i, curr, ans)\r\n                curr.pop()\r\n        \r\n        ans = []        \r\n        candidates.sort()        \r\n        dfs(candidates, target, 0, [], ans);\r\n        \r\n        return ans<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a\u00a0set\u00a0of candidate numbers (C)\u00a0(without duplicates)\u00a0and a target number (T), find all unique combinations in\u00a0C\u00a0where the candidate numbers sums to\u00a0T. The\u00a0same\u00a0repeated number may be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[122,33,121],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-searching","tag-combination","tag-dfs","tag-permutation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/531","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=531"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":2616,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/531\/revisions\/2616"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}