14ms c++ solution


  • 0
    Z

    my solution using c++

        void get_combine(vector<vector<int>> &comb, vector<int> &inst, int n, int k, int pos)
    {
        if(pos == k){
            comb.push_back(inst);
        }else{
            int start = (pos == 0) ? 1 : inst[pos-1] + 1;
            for(; start <= n; start++){
                inst[pos] = start;
                get_combine(comb, inst, n, k, pos + 1);
            }
        }
    }
    
    vector<vector<int> > combine(int n, int k) {
        vector<vector<int>> ret;
        vector<int> tmp(k);
        
        if(n < k || k <= 0)   return ret;
        
        get_combine(ret, tmp, n, k, 0);
        
        return ret;
    }

Log in to reply
 

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