Combinations

Medium

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

Example:

Input:
 n = 4, k = 2

Output:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

Solution

Backtracking

backtracking模板的应用,这里的条件备选方案不重复,排过序(1,2,3,... n)

DFS

排列类搜索的递归方式,每一次递归决定Index选或者不选

Last updated

Was this helpful?