执行用时:4 ms, 在所有 C++ 提交中击败了91.98%的用户
内存消耗:10.5 MB, 在所有 C++ 提交中提交中击败了90.06%的用户
示例一: 输入:candidates = [2,3,6,7], target = 7 输出:[[2,2,3],[7]] 解释: 2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。 7 也是一个候选, 7 = 7 。 仅有这两种组合。
示例二: 输入: candidates = [2,3,5], target = 8 输出: [[2,2,2,2],[2,3,3],[3,5]]
示例三: 输入: candidates = [2], target = 1 输出: []
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/combination-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码如下(示例):
class Solution {
public:
vector> v;
vector path;
vector> combinationSum(vector& candidates, int target) {
combin(candidates,target,0);
return v;
}
void combin(vector& candidates,int target,int index)
{
if(target<=0)
{
if(target==0)
{
v.push_back(path);
}
else
{
return;
}
}
for(int i=index;i 


