easy C++ solution


  • 0
    M
        void _combine(vector<vector<int>>& result, vector<int>& level, int n, int k, int begin)
        {
            if(level.size() == k)
            {
                result.push_back(level);
            }
            
            for(int i=begin;i<=n;i++)
            {
                level.push_back(i);
                _combine(result,level,n,k,i+1);
                level.pop_back();
            }
            
        }
        vector<vector<int>> combine(int n, int k) {
            
            vector<vector<int>> result;
            vector<int> level;
            
            _combine(result,level,n,k,1);
            
            return result;
        }
    

Log in to reply
 

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