Leetcode:Combination Sum 子集和问题

Combination Sum

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] 

 

解题分析:

class Solution {
public:
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        vector<vector<int>> result;
        if (candidates.size() == 0) return result;
        sort(candidates.begin(), candidates.end()); // sort
        vector<int> path;
        dfs(candidates, target, 0, path, result);
        return result;
    }
    
    void dfs(const vector<int>& nums, int gap, int start, vector<int>& path, vector<vector<int>>& result) {
        if (gap == 0) {   // 找到一个合法解
            result.push_back(path);
            return;
        }
        
        for (int i = start; i < nums.size(); ++i) {  // 扩展状态
            if (gap < nums.at(i)) continue; // 剪枝       
            path.push_back(nums.at(i));     // 执行扩展动作 
            dfs(nums, gap - nums.at(i), i, path, result);
            path.pop_back();            // 撤销扩展动作
        }
    }
};

 

Combination Sum II

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] 
class Solution {
public:
    vector<vector<int> > combinationSum2(vector<int> &num, int target) {
        vector<vector<int>> result;
        if (num.size() == 0) return result;
        sort(num.begin(), num.end());
        vector<int> path;
        dfs(num, target, 0, path, result);
        return result;
    }
    
    void dfs(const vector<int>& num, int gap, int start, vector<int>& path, vector<vector<int>>& result) {
        if (gap == 0) {   // 找到一个合法解
            result.push_back(path);
            return;
        }
        int prev = -1;
        for (int i = start; i < num.size(); ++i) {
            if (prev == num.at(i)) continue;  // 确保num.at(i) 最多只用一次
            if (gap < num.at(i)) return;      // 剪枝
            prev = num.at(i);
            
            path.push_back(num.at(i));       // 执行扩展动作
            dfs(num, gap - num.at(i), i + 1, path, result);
            path.pop_back();                 // 撤消扩展动作
        }
    }
};

 

你可能感兴趣的:(LeetCode)