javascript solution with backtracking


  • 0
    G

    '''
    var combine = function(n, k) {

    var res = [];
    var item = [];
    
    function fn(res,item,i,n,k) {
    
    if ( k === 0) {
        var temp = [];
        var len = item.length;
        for (var m = 0; m < len; ++ m) {
            temp[m] = item[m];
        }
        res.push(temp);
    } else {
        for (var j = i; j <= n - ( k - 1); ++ j) {
            item.push(j);
            fn(res, item, j + 1, n, k - 1);
            item.splice(item.length -1);
        }
    }
    

    }

    fn(res, item, 1, n, k);
    return res;
    

    };
    '''


Log in to reply
 

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