My solution using recursion


  • 0
    V
    class Solution {
    private:
        vector<int> temp;
        vector<vector<int>> result;
    public:
        vector<vector<int>> combine(int n, int k) {
            combine( 1, n, k );
            return result;
        }
    
        void combine( int begin, int n, int k ) {
            if( !k )
            {
                result.push_back( temp );
                return;
            }
        
            for( int i = begin; i <= n; i++ )
            {
                temp.push_back( i );
                combine( i + 1, n, k - 1 );
                temp.pop_back();
            }
        
        }
    
    };

Log in to reply
 

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