[leetcode]77. Combinations


Analysis

waiting~—— [每天刷题并不难0.0]

Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
[leetcode]77. Combinations
排列和组合的问题好像一般都可以用递归来解决~

Implement

class Solution {
public:
    vector<vector<int>> combine(int n, int k) {
        vector<int> tmp;
        helper(tmp, 1, n, k);
        return res;
    }
    void helper(vector<int>& tmp, int start, int n, int k){
        if(k == 0){
            res.push_back(tmp);
            return ;
        }
        for(int i=start; i<=n; i++){
            tmp.push_back(i);
            helper(tmp, i+1, n, k-1);
            tmp.pop_back();
        }
    }
private:
    vector<vector<int>> res;
};

相关文章:

  • 2022-12-23
  • 2022-01-27
  • 2021-07-16
  • 2021-05-03
  • 2022-02-08
  • 2022-03-01
猜你喜欢
  • 2021-07-21
  • 2021-04-25
  • 2021-04-04
  • 2022-01-27
  • 2021-07-04
  • 2022-02-13
  • 2021-08-10
相关资源
相似解决方案