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

题解:

排序后Backtracking。

C++版:

class Solution {
public:
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<vector<int>> results;
        sort(candidates.begin(), candidates.end());
        for(int i = 0; i < candidates.size(); i++) {
            vector<int> result;
            result.push_back(candidates[i]);
            combination(candidates, results, result, target - candidates[i], i);
            result.pop_back();
        }
        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);
            result.pop_back();
        }
    }
};

Java版:

public class Solution {
    public List<List<Integer>> combinationSum(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);
            result.remove(result.size() - 1);
        }
        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);
            result.remove(result.size() - 1);
        }
    }
}

Python版:

import copy

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

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