{"id":2174,"date":"2016-09-09T16:56:25","date_gmt":"2016-09-09T08:56:25","guid":{"rendered":"http:\/\/boweihe.me\/?p=2174"},"modified":"2016-09-09T16:56:25","modified_gmt":"2016-09-09T08:56:25","slug":"leetcode-39-combination-sum","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2174","title":{"rendered":"LeetCode 39. Combination Sum"},"content":{"rendered":"<h3>\u601d\u8def<\/h3>\n<p>\u56de\u6eaf<br \/>\n\u5148\u628acandidate\u6392\u4e2a\u5e8f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u4e2d\u9014\u76f4\u63a5cut\u6389\u540e\u9762\u7684loop<br \/>\n\u4e3a\u4e86\u907f\u514d\u91cd\u590d\u7ed3\u679c\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2astartIndex\uff0c\u5728\u8fd9\u4e4b\u524d\u7684\u90fd\u4e0d\u80fd\u4f5c\u4e3acandidate<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    void combSumSub(vector&lt;int&gt;&amp; candidates, int target, vector&lt;vector&lt;int&gt;&gt;&amp; results, vector&lt;int&gt; currPath, int candStartIndex){\n        for(int i=candStartIndex; i&lt;candidates.size(); i++){\n            if(target - candidates[i] &lt; 0)\n                break; \/\/too big\n            if(target == candidates[i]){\n                currPath.push_back(candidates[i]);\n                results.push_back(currPath);\n                continue;\n            }\n            currPath.push_back(candidates[i]);\n            combSumSub(candidates, target - candidates[i], results, currPath, i);\n            currPath.erase(currPath.end()-1);\n        }\n    }\n    vector&lt;vector&lt;int&gt;&gt; combinationSum(vector&lt;int&gt;&amp; candidates, int target) {\n        sort(candidates.begin(), candidates.end());\n        vector&lt;vector&lt;int&gt;&gt; result;\n        combSumSub(candidates, target, result, vector&lt;int&gt;(), 0);\n        return result;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def \u56de\u6eaf \u5148\u628acandidate\u6392\u4e2a\u5e8f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u4e2d\u9014\u76f4\u63a5cut\u6389\u540e\u9762\u7684loop \u4e3a\u4e86\u907f\u514d\u91cd\u590d\u7ed3\u679c\uff0c\u9700\u8981\u4f20\u5165\u4e00\u4e2astartIndex\uff0c\u5728\u8fd9\u4e4b\u524d\u7684\u90fd\u4e0d\u80fd\u4f5c\u4e3acandidate \u4ee3\u7801 class Solution { public: void combSumSub(vector&lt;int&gt;&amp; candidates, int target, vector&lt;vector&lt;int&gt;&gt;&amp; results, vector&lt;int&gt; currPath, int candStartIndex){ for(int i=candStartIndex; i&lt;candidates.size(); i++){ if(target &#8211; candidates[i] &lt; 0) break; \/\/too big if(target == candidates[i]){ currPath.push_back(candidates[i]); results.push_back(currPath); continue; } currPath.push_back(candidates[i]); combSumSub(candidates, target &#8211; candidates[i], results, currPath, i); currPath.erase(currPath.end()-1); } } vector&lt;vector&lt;int&gt;&gt; combinationSum(vector&lt;int&gt;&amp; candidates, int target) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,144],"class_list":["post-2174","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-144"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2174"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2174\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}