Combination Sum I&II&III
in 剑指offer with 0 comment

Combination Sum I&II&III

in 剑指offer with 0 comment
这种套餐题,做起来就感觉很爽,简直跟套娃一样,一环套一环。

题目链接

第一题

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

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

Example 1:

Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
  [7],
  [2,2,3]
]

特点

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

代码

class Solution {
public:
    void combination(vector<int>& candidates,vector<vector<int>>& result, vector<int> sol, int target,int pos) {
        if (target == 0) { result.push_back(sol); return; }
        for (int i = pos; i < candidates.size();i++) {
            if (target < candidates[i]) continue;
            sol.push_back(candidates[i]);
            combination(candidates, result, sol, target - candidates[i],i);
            sol.pop_back();
        }
    }

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>>  ans;
        vector<int> sol;
        combination(candidates, ans, sol, target, 0);
        return ans;
    }
};

每个数字可以使用无数次,所以使用 pos 记录上次使用的数字,如果需要无数次使用。

第二题

特点

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

特点

class Solution {
public:
    void combination(vector<int>& candidates, vector<vector<int>>& result, vector<int> sol, int target, int pos) {
        if (target == 0) {
            sort(sol.begin(), sol.end());
            auto it = find(result.begin(), result.end(), sol);
            if(it==result.end())
                result.push_back(sol); 
            return; 
        }
        for (int i = pos; i < candidates.size(); i++) {
            if (target < candidates[i]) continue;
            sol.push_back(candidates[i]);
            combination(candidates, result, sol, target - candidates[i], i + 1);
            sol.pop_back();
        }
    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>>  ans;
        vector<int> sol;
        combination(candidates, ans, sol, target, 0);
        return ans;
    }
};

posi+1 获得,避免重复使用同一个元素,但是I内说明:without duplicatesII没有说

所以.... 还得手动检查一下 sol 是不是在 result 里边了

第三题

特点

Find all possible combinations of k numbers that add up to a number n
k 个数字组合成 n

代码

class Solution {
public:
    void combination(vector<vector<int>>& result, vector<int> sol, int k, int n) {
        if (sol.size() == k&&n == 0) { result.push_back(sol); return; }
        if (sol.size() < k) {
            for (int i = sol.empty() ? 1 : sol.back() + 1; i <= 9; i++) {
                if (n < i)break;
                sol.push_back(i);
                combination(result, sol, k, n - i);
                sol.pop_back();
            }
        }
    }

    vector<vector<int>> combinationSum3(int k, int n) {
        vector<vector<int>> ans;
        vector<int> sol;
        combination(ans, sol, k, n);
        return ans;
    }
};

边缘条件从 target==0 变成了 sol.size()==k

小结

大致结题步骤

Responses

From now on, bravely dream and run toward that dream.
陕ICP备17001447号