LeetCode 40. Combination Sum II compositions sum II (C ++ / Java)

topic:

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

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

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]

Example 2:

Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
  [1,2,2],
  [5]
]

analysis:

This problem is LeetCode 39. Combination Sum the total composition (C ++ / Java) extension, we use the method to do this question 39 in question.

But this question to the figures given duplicate, and require the final result can not be repeated, e.g. example 1, there are two 1, 7, and they can be combined into 8, if the title is generated according to the method 39 is repeated to search result.

One way is to generate search results added to the set, so that collections will automatically help us de-emphasis.

In addition, we also can search in every round, when found to have the same elements, skip this search elements, so as not to produce duplicate the results.

program:

C++

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> res;
        vector<int> curr;
        sort(candidates.begin(), candidates.end());
        dfs(candidates, target, 0, res, curr);
        return res;
    }
    void dfs(vector<int>& candidates, int target, int index, vector<vector<int>>& res, vector<int> curr){
        if(target == 0){
            res.push_back(curr);
            return;
        }
        for(int i = index; i < candidates.size(); ++i){
            if(candidates[i] > target)
                return;
            if(i > index && candidates[i] == candidates[i-1])
                continue;
            curr.push_back(candidates[i]);
            dfs(candidates, target-candidates[i], i+1, res, curr);
            curr.pop_back();
        }
    }
};

Java

class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<Integer> curr = new LinkedList<>();
        Arrays.sort(candidates);
        dfs(candidates, target, 0, res, curr);
        return res;
    }
    private void dfs(int[] candidates, int target, int index, List<List<Integer>> res, LinkedList<Integer> curr){
        if(target == 0){
            res.add(new LinkedList<>(curr));
            return;
        }
        for(int i = index; i < candidates.length; ++i){
            if(candidates[i] > target)
                return;
            if(i > index && candidates[i] == candidates[i-1])
                continue;
            curr.addLast(candidates[i]);
            dfs(candidates, target-candidates[i], i+1, res, curr);
            curr.removeLast();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/silentteller/p/12372194.html