39. Combination Sum

1. 每个元素可用多次

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

 For example, given candidate set 2,3,6,7 and target 7

A solution set is: 
[7] 
[2, 2, 3] 

void cal(vector<int>& candidates, int start, int target, vector<int>& v, vector<vector<int>>& ans)
{
    if(0 == target)
    {
        ans.push_back(v);
        return;
    }
    if(target < 0)
        return;
    int n = candidates.size(), i;
    for(i = start; i < n; i++)
    {
        if(i > start && candidates[i] == candidates[i-1])
            continue;
        v.push_back(candidates[i]);
        cal(candidates, i, target-candidates[i], v, ans);
        v.pop_back();
    }
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
    vector<vector<int>> ans;
    vector<int> v;
    sort(candidates.begin(), candidates.end());
    cal(candidates, 0, target, v, ans);
    return ans;
}

 

2. 每个元素只能用一次

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.

 For example, given candidate set 10,1,2,7,6,1,5 and target 8

A solution set is: 
[1, 7] 
[1, 2, 5] 
[2, 6] 
[1, 1, 6] 

void cal(vector<int>& candidates, int start, int target, vector<int>& v, vector<vector<int>>& ans)
{
    if(0 == target)
    {
        ans.push_back(v);
        return;
    }
    if(target < 0)
        return;
    int n = candidates.size(), i;
    for(i = start; i < n; i++)
    {
        if(i > start && candidates[i] == candidates[i-1])
            continue;
        v.push_back(candidates[i]);
        cal(candidates, i+1, target-candidates[i], v, ans);
        v.pop_back();
    }
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
    vector<vector<int>> ans;
    vector<int> v;
    sort(candidates.begin(), candidates.end());
    cal(candidates, 0, target, v, ans);
    return ans;
}

if(i > start && candidates[i] == candidates[i-1])continue;中的i > start可以保证没有重复结果。

 

3. 数字范围1-9,给定个数。

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example 1:

Input: k = 3, n = 7

Output:

 [[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]
void cal(vector<int>& candidates, int start, int target, int k, vector<int>& v, vector<vector<int>>& ans)
{
    if(0 == target && 0 == k)
    {
        ans.push_back(v);
        return;
    }
    if(target < 0 || 0 == k)
        return;
    int n = candidates.size(), i;
    for(i = start; i < n; i++)
    {
        if(i > start && candidates[i] == candidates[i-1])
            continue;
        v.push_back(candidates[i]);
        cal(candidates, i+1, target-candidates[i], k-1, v, ans);
        v.pop_back();
    }
}
vector<vector<int>> combinationSum3(int k, int target) {
    vector<vector<int>> ans;
    vector<int> candidates, v;
    for(int i = 1; i <= 9; i++)
        candidates.push_back(i);
    cal(candidates, 0, target, k, v, ans);
    return ans;
}

 

你可能感兴趣的:(39. Combination Sum)