Leetcode 77. Combinations
阿新 • • 發佈:2018-11-09
文章作者:Tyan
部落格:noahsnail.com | CSDN | 簡書
1. Description
2. Solution
class Solution { public: vector<vector<int>> combine(int n, int k) { vector<vector<int>> result; vector<int> combination; combine(result, combination, n, k, 1); return result; } void combine(vector<vector<int>>& result, vector<int>& combination, int& n, int& k, int begin) { if(n - begin + 1 + combination.size() < k) { return; } if(combination.size() == k) { result.push_back(combination); return; } for(int i = begin; i <= n; i++) { combination.push_back(i); combine(result, combination, n, k, i + 1); combination.pop_back(); } } };