首页 > 代码库 > LeetCode Combinations

LeetCode Combinations

class Solution {private:    vector<vector<int> > res;public:    vector<vector<int> > combine(int n, int k) {        res.clear();        vector<int> path;        dfs(1, n, k, path);        return res;    }        void dfs(int lower, int upper, int level, vector<int>& path) {        if (level == 0) {            res.push_back(path);            return;        }        for (int i=lower; i<= upper - level + 1; i++) {            path.push_back(i);            dfs(i + 1, upper, level -1, path);            path.pop_back();        }    }};

常规dfs