c++ recursive solution


  • 0
    B
    class Solution {
    public:
        vector<vector<int>> result;
        
        void helper(vector<int> cur, int start, int n, int k){
            if(cur.size()==k){
                result.push_back(cur);
                return ;
            }
            
            for(int i=start;i<=n;i++){
                cur.push_back(i);
                helper(cur, i+1, n, k);
                cur.pop_back();
            }
            
            return ;
        }
        
        vector<vector<int>> combine(int n, int k) {
            vector<int> cur;
            helper(cur, 1, n, k);
            return result;
        }
    };
    
    

Log in to reply
 

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