Concise Javascript solution


  • 0
    S
    /**
     * @param {number} n
     * @param {number} k
     * @return {number[][]}
     */
    var combine = function(n, k) {
        return helper(1, n, k);
    };
    
    function helper (start, numbers, k, combination = []) {
        var result = [];
    
    
        if (k === 0)
            return [combination];
    
        for (let i = start; i <= numbers; i++) {
    
            // Check to see if there is enough space to make combination of k numbers
            if (numbers  - i + 1 < k) {
                continue;
            }
    
            result = result.concat(helper(i + 1, numbers, k - 1, combination.concat(i)));
        }
    
        return result;
    }
    

Log in to reply
 

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