Combinations
最后更新于:2022-04-02 01:12:27
# Combinations
### Source
- leetcode: [Combinations | LeetCode OJ](https://leetcode.com/problems/combinations/)
- lintcode: [(152) Combinations](http://www.lintcode.com/en/problem/combinations/)
~~~
Given two integers n and k,
return all possible combinations of k numbers out of 1 ... n.
Example
For example,
If n = 4 and k = 2, a solution is:
[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4]]
~~~
### 题解
套用 [Permutations](http://algorithm.yuanbin.me/zh-cn/exhaustive_search/permutations.html) 模板。
### Java
~~~
public class Solution {
/**
* @param n: Given the range of numbers
* @param k: Given the numbers of combinations
* @return: All the combinations of k numbers out of 1..n
*/
public List
';
- > combine(int n, int k) {
List
- > result = new ArrayList
- >();
List
- > result) {
if (list.size() == k) {
result.add(new ArrayList