Recursive solution, JS


  • 0
    R
    var res; 
    var combine = function(n, k) {
        res = [];
        if(k !== 0)
            combineRec([],n,k)
        return res;
    };
    
    function combineRec(arr,n, k) {
        lastElm = arr[arr.length-1];
        if(k === 0)
            res.push(arr);
        else if(lastElm !== n) {
            start = (lastElm === undefined ? 1 : lastElm + 1);
            for(var i = start; i <= n ; i++) 
                combineRec(arr.concat(i),n,k-1);
        }
    }

Log in to reply
 

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