A javascript solution beats 100%


  • 0
    H
    var combine = function(n, k) {
        if(k > n) {
            return []
        }
        var res = []
        var comb = function(d, n, k, arr) {
            if(k === 0) {
                res.push(arr.slice(0))
                return
            }
            for(var i = n; i >= k; i--) {
                arr[d] = i
                comb(d + 1, i - 1, k - 1, arr)
            }
        }
        comb(0, n, k, [])
        return res
    };
    

Log in to reply
 

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