LeetCode 题解(175): 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, a1a2 ≤ … ≤ 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]

题解:

与Combination Sum I 的区别是元素不能重复使用,结果中不能有重复结果。所以要跳过排序后重复的元素。

C++版:

class Solution {
public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<vector<int>> results;
        sort(candidates.begin(), candidates.end());
        int i = 0;
        while(i < candidates.size()) {
            vector<int> result;
            result.push_back(candidates[i]);
            combination(candidates, results, result, target - candidates[i], i + 1);
            result.pop_back();
            while(candidates[i+1] == candidates[i])
                i++;
            i++;
        }
        return results;
    }
    
    void combination(vector<int>& candidates, vector<vector<int>>& results, vector<int>& result, int target, int i) {
        if(target < 0)
            return;
        if(target == 0) {
            results.push_back(result);
            return;
        }
        for(int j = i; j < candidates.size(); j++) {
            result.push_back(candidates[j]);
            combination(candidates, results, result, target - candidates[j], j + 1);
            result.pop_back();
            while(candidates[j] == candidates[j+1])
                j++;
        }
    }
};

Java版:

public class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<List<Integer>> results = new ArrayList<List<Integer>>();
        for(int i = 0; i < candidates.length; i++) {
            List<Integer> result = new ArrayList<>();
            result.add(candidates[i]);
            combination(candidates, results, result, target - candidates[i], i + 1);
            result.remove(result.size() - 1);
            while(i + 1 < candidates.length && candidates[i] == candidates[i + 1])
                i++;
        }
        return results;
    }
    
    public void combination(int[] candidates, List<List<Integer>> results, List<Integer> result, int target, int i) {
        if(target < 0)
            return;
        if(target == 0) {
            List<Integer> temp = new ArrayList<>();
            temp.addAll(result);
            results.add(temp);
        }
        for(int j = i; j < candidates.length; j++) {
            result.add(candidates[j]);
            combination(candidates, results, result, target - candidates[j], j + 1);
            result.remove(result.size() - 1);
            while(j + 1 < candidates.length && candidates[j] == candidates[j + 1])
                j++;
        }
    }
}

Python版:

import copy

class Solution:
    # @param {integer[]} candidates
    # @param {integer} target
    # @return {integer[][]}
    def combinationSum2(self, candidates, target):
        results, result = [], []
        candidates.sort()
        i = 0
        while i < len(candidates):
            result.append(candidates[i])
            self.combination(candidates, results, result, target - candidates[i], i + 1)
            result.pop()
            while i + 1 < len(candidates) and candidates[i] == candidates[i+1]:
                i += 1
            i += 1
        return results
        
    def combination(self, candidates, results, result, target, i):
        if target < 0:
            return
        if target == 0:
            temp = copy.copy(result)
            results.append(temp)
        j = i
        while j < len(candidates):
            result.append(candidates[j])
            self.combination(candidates, results, result, target - candidates[j], j + 1)
            result.pop()
            while j + 1 < len(candidates) and candidates[j] == candidates[j+1]:
                j += 1
            j += 1

你可能感兴趣的:(LeetCode,Algorithm,面试题)