LeetCode #216 Combination Sum III 组合总和 III

216 Combination Sum III 组合总和 III

Description:
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.

Note:

All numbers will be positive integers.
The solution set must not contain duplicate combinations.

Example:

Example 1:

Input: k = 3, n = 7
Output: [[1,2,4]]

Example 2:

Input: k = 3, n = 9
Output: [[1,2,6], [1,3,5], [2,3,4]]

题目描述:
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。

说明:

所有数字都是正整数。
解集不能包含重复的组合。

示例 :

示例 1:

输入: k = 3, n = 7
输出: [[1,2,4]]
示例 2:

输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]

思路:

回溯法
终点为长度等于 n且 target == 0
时间复杂度O(n!), 空间复杂度O(n)

代码:
C++:

class Solution 
{
public:
    vector> combinationSum3(int k, int n) 
    {
        vector> result;
        vector track;
        trackback(result, track, k, 1, n);
        return result;
    }
private:
    void trackback(vector>& result, vector& track, int k, int start, int target) 
    {
        if (target < 0) return;
        if (track.size() == k and target == 0) 
        {
            result.push_back(track);
            return;
        }
        for (int i = start; i < 10; i++) 
        {
            track.push_back(i);
            trackback(result, track, k, i + 1, target - i);
            track.pop_back();
        }
    }
};

Java:

class Solution {
    public List> combinationSum3(int k, int n) {
        List> result = new ArrayList<>();
        trackback(result, new ArrayList(), k, 1, n);
        return result;
    }
    
    private void trackback(List> result, List temp, int k, int start, int target) {
        if (target < 0) return;
        if (temp.size() == k && target == 0) {
            result.add(new ArrayList<>(temp));
            return;
        }
        for (int i = start; i < 10; i++) {
            temp.add(i);
            trackback(result, temp, k, i + 1, target - i);
            temp.remove(temp.size() - 1);
        }
    }
}

Python:

class Solution:
    def combinationSum3(self, k: int, n: int, r=range(1, 10)) -> List[List[int]]:
        return [list(i) for i in itertools.combinations(range(1, 10), k) if sum(i) == n]

你可能感兴趣的:(LeetCode #216 Combination Sum III 组合总和 III)