美文网首页Leetcode
Leetcode 216. Combination Sum II

Leetcode 216. Combination Sum II

作者: SnailTyan | 来源:发表于2018-10-17 18:11 被阅读1次

    文章作者:Tyan
    博客:noahsnail.com  |  CSDN  |  简书

    1. Description

    Combination Sum III

    2. Solution

    class Solution {
    public:
        vector<vector<int>> combinationSum3(int k, int n) {
            vector<vector<int>> result;
            vector<int> combination;
            combinationSum3(result, combination, n, k, 1);
            return result;
        }
    
    
    private:
        void combinationSum3(vector<vector<int>>& result, vector<int>& combination, int n, int& k, int begin) {
            const int END = 10;
            if(n < 0 || combination.size() > k) {
                return;
            }
            if(n == 0 && combination.size() == k) {
                result.push_back(combination);
                return;
            }
            for(int i = begin; i < END; i++) {
                combination.push_back(i);
                combinationSum3(result, combination, n - i, k, i + 1);
                combination.pop_back();
            }
        }
    };
    

    Reference

    1. https://leetcode.com/problems/combination-sum-iii/description/

    相关文章

      网友评论

        本文标题:Leetcode 216. Combination Sum II

        本文链接:https://www.haomeiwen.com/subject/xsylzftx.html