欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

从m个数中取n个数

程序员文章站 2024-02-27 15:11:21
...
    void dfs(vector<int> &nums, int m, int k, vector<int> &pick){
        for(int i = m; i >= k; i--){
            pick.push_back(nums[i-1]);
            if(k > 1){
                dfs(nums, i-1, k-1, pick);
            }else{
                ans = min(ans, pick);
            }
            pick.pop_back();
        }
    }

 

    void dfs_(vector<int> &nums, int n, int k, int idx, vector<int> &pick){
        if(k == 0){
            for(auto it : pick) cout << it << " ";
            cout << endl;
            return;
        }
        for(int i = idx; i <= n-k; i++){
            pick.push_back(nums[i]);
            dfs_(nums, n, k-1, i+1, pick);
            pick.pop_back();
        }
    }

 

 
相关标签: Leetcode