{ "type": "code_options", "author": "https://github.com/begeekmyfriend/leetcode", "source": "solution.md", "exercise_id": "28697aa8d9b94ea4877b314637b97fbe", "keywords": "数组,回溯", "title": "组合总和", "desc": [ { "content": "\n

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。\n

\n

candidates 中的数字可以无限制重复被选取。

\n

说明:

\n\n

示例 1:

\n
输入:candidates = [2,3,6,7], target = 7,
输出:
[[7],[2,2,3]]
\n

示例 2:

\n
输入:candidates = [2,3,5], target = 8,
输出:
[[2,2,2,2],[2,3,3],[3,5]]
\n

 

\n

提示:

\n", "language": "markdown" } ], "answer": [ { "content": "", "language": "cpp" } ], "prepared": [ [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ], [ { "content": "", "language": "cpp" } ] ], "template": { "content": "#include \nusing namespace std;\nclass Solution\n{\npublic:\n\tvector> combinationSum(vector &candidates, int target)\n\t{\n\t\tvector> res;\n\t\tdfs(candidates, 0, target, res);\n\t\treturn res;\n\t}\nprivate:\n\tvector stack;\n\tvoid dfs(vector &candidates, int start, int target, vector> &res)\n\t{\n\t\tif (target < 0)\n\t\t{\n\t\t\treturn;\n\t\t}\n\t\telse if (target == 0)\n\t\t{\n\t\t\tres.push_back(stack);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfor (int i = start; i < candidates.size(); i++)\n\t\t\t{\n\t\t\t\tstack.push_back(candidates[i]);\n\t\t\t\tdfs(candidates, i, target - candidates[i], res);\n\t\t\t\tstack.pop_back();\n\t\t\t}\n\t\t}\n\t}\n};", "language": "cpp" }, "node_id": "dailycode-58550dadc0174b719c8769c5d87c8814", "license": "csdn.net", "created_at": 1637894158, "topic_link": "https://bbs.csdn.net/topics/600471017" }