Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 737 Bytes

Combinations.md

File metadata and controls

31 lines (26 loc) · 737 Bytes

Given two integers n and k, return all possible combinations of k numbers out of the range [1, n].

You may return the answer in any order.

Screen Shot 2021-11-23 at 21 28 53

/**
 * @param {number} n
 * @param {number} k
 * @return {number[][]}
 */
var combine = function(n, k) {
    let ans = comb(n, k);
    return ans;
};

let comb = function(n, k, res = [], curr = [], index = 1) {
    if(curr.length === k) {
        res.push(curr);
        return;
    }
    else {
        while(index <= n) {
            comb(n, k, res, [...curr, index], ++index);
        }
        return res
    }
}