当前位置:编程学习 > JAVA >>

[leetcode]Combinations

class Solution {
    vector<vector<int>> result;
    
public:
    void dfs(vector<int> &tmp, int level, int n, int k){
        if(level > n){
            if(tmp.size() == k){
                result.push_back(tmp);
            }
            return;
        }
        dfs(tmp, level+1, n, k);
        tmp.push_back(level);
        dfs(tmp, level+1, n, k);
        tmp.pop_back();
        
    }

    vector<vector<int> > combine(int n, int k) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        result.clear();
        vector<int> tmp;
        dfs(tmp, 1, n, k);
        return result;
        
    }
};

 

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,