Clean solution in C++, Efficient Enough


  • -2
    class Solution {
    private:
        void search(int n, int pos, int k, vector<int>&v, vector<vector<int>>& vv)
        {
            if(k == 0) 
            {
                vv.push_back(v);
                return ;
            }
            for(int i = pos; i <= n-k; ++i)
            {
                v.push_back(i);
                search(n, i+1, k-1, v, vv);
                v.pop_back();
            }
        }
    public:
        vector<vector<int>> combine(int n, int k) 
        {
            vector<int> v;
            vector<vector<int>> vv;
            search(n, 1, k, v, vv);
            return vv;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.