8ms simple C++ code


  • 3
    L
    class Solution {
    public:
        vector<vector<int>> combine(int n, int k) 
        {
           vector<vector<int>> ret;
           if(n==0||k==0)return ret;
           vector<int> stack;
           combine(n, k,stack,ret, 1);
           return ret;
        }
        
        
        
    private:
        void combine(int n, int k,vector<int> &stack,vector<vector<int>> &ret,int start) 
        {
            if(0==k) 
            {
                ret.push_back(stack);
                return;
            }
            for(int i=start;i<=n-k+1;i++)
            {
                stack.push_back(i);
                combine(n, k-1,stack,ret, i+1);
                stack.pop_back();
            }
        }
    
    
    };

Log in to reply
 

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