leetcode Combination Sum

来源:互联网 发布:阿里云存储服务 个人 编辑:程序博客网 时间:2024/06/02 03:38

1、
Given a set of candidate numbers (C) (without duplicates) 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.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) {        std::sort(candidates.begin(), candidates.end());        vector<vector<int>> res;        vector<int> combination;        combinationSum(candidates, target, res, combination, 0);        return res;    }    void combinationSum(vector<int>& candidates, int target, vector<vector<int>>& res, vector<int>& combination, int begin) {        if (!target) {            res.push_back(combination);            return;        }        for (int i = begin; i < candidates.size() && candidates[i] <= target; ++i) {            combination.push_back(candidates[i]);            combinationSum(candidates, target-candidates[i], res, combination, i);            combination.pop_back();        }    }};

2、变形,数组中每个元素最多用一次

class Solution {public:    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {        std::sort(candidates.begin(), candidates.end());        vector<vector<int>> res;        vector<int> combination;        combinationSum2(candidates, target, res, combination, 0);        return res;    }    void combinationSum2(vector<int>& candidates, int target, vector<vector<int>>& res, vector<int>& combination, int begin) {        if (!target) {            res.push_back(combination);            return;        }        for (int i = begin; i < candidates.size() && candidates[i] <= target; ++i) {            if (i == begin || candidates[i] != candidates[i-1]){//保证不出现相同的组合                combination.push_back(candidates[i]);                combinationSum2(candidates, target-candidates[i], res, combination, i+1);//i+1保证每个元素用一次                combination.pop_back();            }        }    }};

3、

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.

  • Example : Input: k = 3, n = 7
  • Output: [[1,2,4]]
    题意:给定目标数n,从1~9中找出k个数使得它们的和为n。
class Solution {public:    vector<vector<int>> combinationSum3(int k, int n) {        vector<vector<int>> res;        vector<int> combination;        combinationSum3(n, res, combination, 1, k);        return res;    }    void combinationSum3(int target, vector<vector<int>>& res, vector<int>& combination, int begin, int need) {        if (!target) {            res.push_back(combination);            return;        }        else if (!need) {            return;        }        for (int i = begin; i != 10 && target >= i * need + need * (need-1) / 2; ++i) {//保证target大于最小的need个数的和            combination.push_back(i);            combinationSum3(target-i, res, combination, i+1, need-1);            combination.pop_back();        }    }};