Given two integers n
and k
, return all possible combinations of k
numbers chosen from the range [1, n]
.
You may return the answer in any order.
Example 1:
Input: n = 4, k = 2 Output: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] Explanation: There are 4 choose 2 = 6 total combinations. Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
Example 2:
Input: n = 1, k = 1 Output: [[1]] Explanation: There is 1 choose 1 = 1 total combination.
Constraints:
1 <= n <= 20
1 <= k <= n
DFS.
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
def dfs(i, n, k, t):
if len(t) == k:
res.append(t.copy())
return
for j in range(i, n + 1):
t.append(j)
dfs(j + 1, n, k, t)
t.pop()
dfs(1, n, k, [])
return res
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
dfs(1, n, k, new ArrayList<>(), res);
return res;
}
private void dfs(int i, int n, int k, List<Integer> t, List<List<Integer>> res) {
if (t.size() == k) {
res.add(new ArrayList<>(t));
return;
}
for (int j = i; j <= n; ++j) {
t.add(j);
dfs(j + 1, n, k, t, res);
t.remove(t.size() - 1);
}
}
}
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> res;
vector<int> t;
dfs(1, n, k, t, res);
return res;
}
void dfs(int i, int n, int k, vector<int> t, vector<vector<int>>& res) {
if (t.size() == k) {
res.push_back(t);
return;
}
for (int j = i; j <= n; ++j) {
t.push_back(j);
dfs(j + 1, n, k, t, res);
t.pop_back();
}
}
};
func combine(n int, k int) [][]int {
var res [][]int
var t []int
dfs(1, n, k, t, &res)
return res
}
func dfs(i, n, k int, t []int, res *[][]int) {
if len(t) == k {
cp := make([]int, k)
copy(cp, t)
*res = append(*res, cp)
return
}
for j := i; j <= n; j++ {
t = append(t, j)
dfs(j+1, n, k, t, res)
t = t[:len(t)-1]
}
}
function combine(n: number, k: number): number[][] {
const res: number[][] = [];
const dfs = (i: number, t: number[]) => {
if (t.length == k) {
res.push(t);
return;
}
// pruning
if (t.length + n - i + 1 < k) {
return;
}
for (let j = i; j <= n; j++) {
dfs(j + 1, [...t, j]);
}
};
dfs(1, []);
return res;
}
impl Solution {
fn dfs(i: i32, n: i32, k: i32, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
if k == 0 {
res.push(t.clone());
return;
}
// pruning
if n - i + 1 < k {
return;
}
for j in i..=n {
t.push(j);
Self::dfs(j + 1, n, k - 1, t, res);
t.pop();
}
}
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
let mut res = vec![];
Self::dfs(1, n, k, &mut vec![], &mut res);
res
}
}