//给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 
//
// 示例: 
//
// 输入: n = 4, k = 2
//输出:
//[
//  [2,4],
//  [3,4],
//  [2,3],
//  [1,2],
//  [1,3],
//  [1,4],
//] 
// Related Topics 回溯算法 
// 👍 499 👎 0

/*
* 77 组合
* 2021-02-22 16:17:43
* @author oxygenbytes
*/ 
#include "leetcode.h" 
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    vector<vector<int>> ans;
    vector<int> cur;
    vector<vector<int>> combine(int n, int k) {
        if(k > n || k <= 0) return ans;
        dfs(n, k, 1);
        return ans;
    }
    void dfs(int n, int k, int level){
        if(cur.size() == k){
            ans.push_back(cur);
            return ;
        }
        if(n - level + 1 < k - cur.size()) return ; // 剪枝
        for(int i = level;i <= n;i++){
            cur.push_back(i);
            dfs(n, k, i+1);
            cur.pop_back();
        }
    }
};
//leetcode submit region end(Prohibit modification and deletion)