{"id":607,"date":"2017-10-15T20:45:51","date_gmt":"2017-10-16T03:45:51","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=607"},"modified":"2018-04-19T08:39:12","modified_gmt":"2018-04-19T15:39:12","slug":"leetcode-40-combination-sum-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-40-combination-sum-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 40. Combination Sum II"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 40. Combination Sum II - \u5237\u9898\u627e\u5de5\u4f5c EP88\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/RSatA4uVBDQ?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<div class=\"question-description\">\n<p>Given a collection of candidate numbers (<b><i>C<\/i><\/b>) and 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>Each number in\u00a0<b><i>C<\/i><\/b>\u00a0may only be used\u00a0<b>once<\/b>\u00a0in the combination.<\/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>[10, 1, 2, 7, 6, 1, 5]<\/code>\u00a0and target\u00a0<code>8<\/code>,<br \/>\nA solution set is:<\/p>\n<pre class=\"\">[\r\n  [1, 7],\r\n  [1, 2, 5],\r\n  [2, 6],\r\n  [1, 1, 6]\r\n]<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<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 \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/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\/40-ep88.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-610\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/40-ep88.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/40-ep88.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/40-ep88-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/40-ep88-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/10\/40-ep88-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++ \/ set<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    vector&lt;vector&lt;int&gt;&gt; combinationSum2(vector&lt;int&gt;&amp; candidates, int target) {\r\n        set&lt;vector&lt;int&gt;&gt; ans;\r\n        std::sort(candidates.begin(), candidates.end());\r\n        vector&lt;int&gt; curr;\r\n        dfs(candidates, target, 0, ans, curr);\r\n        return vector&lt;vector&lt;int&gt;&gt;(ans.begin(), ans.end());\r\n    }\r\nprivate:\r\n    void dfs(const vector&lt;int&gt;&amp; candidates, \r\n             int target, int s, \r\n             set&lt;vector&lt;int&gt;&gt;&amp; ans,              \r\n             vector&lt;int&gt;&amp; curr) {\r\n        if (target == 0) {\r\n            ans.insert(curr);\r\n            return;\r\n        }\r\n        \r\n        for (int i = s; i &lt; candidates.size(); ++i) {\r\n            int num = candidates[i];\r\n            if (num &gt; target) return;\r\n            curr.push_back(num);\r\n            dfs(candidates, target - num, i + 1, ans, curr);\r\n            curr.pop_back();\r\n        }\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>C++ \/ vector<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 9ms\r\n\r\nclass Solution {\r\npublic:\r\n    vector&lt;vector&lt;int&gt;&gt; combinationSum2(vector&lt;int&gt;&amp; candidates, int target) {\r\n        vector&lt;vector&lt;int&gt;&gt; ans;\r\n        std::sort(candidates.begin(), candidates.end());\r\n        vector&lt;int&gt; curr;\r\n        dfs(candidates, target, 0, ans, curr);\r\n        return ans;\r\n    }\r\nprivate:\r\n    void dfs(const vector&lt;int&gt;&amp; candidates, \r\n             int target, int s, \r\n             vector&lt;vector&lt;int&gt;&gt;&amp; ans,              \r\n             vector&lt;int&gt;&amp; curr) {\r\n        if (target == 0) {\r\n            ans.push_back(curr);\r\n            return;\r\n        }\r\n        \r\n        for (int i = s; i &lt; candidates.size(); ++i) {\r\n            int num = candidates[i];\r\n            if (num &gt; target) return;\r\n            if (i &gt; s &amp;&amp; candidates[i] == candidates[i - 1]) continue;\r\n            curr.push_back(num);\r\n            dfs(candidates, target - num, i + 1, ans, curr);\r\n            curr.pop_back();\r\n        }\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in\u00a0C\u00a0where the candidate numbers sums to\u00a0T. Each number&#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,131,62],"class_list":["post-607","post","type-post","status-publish","format-standard","hentry","category-searching","tag-combination","tag-pruning","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/607","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=607"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":2677,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/607\/revisions\/2677"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}