# LeetCode 77. Combinations题目分析代码

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] Subscribe to see which companies asked this question.

# 代码

```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 ArrayList<ArrayList<Integer>> combine(int n, int k) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();

ArrayList<Integer> list = new ArrayList<>();

if(n < k)
return res;

dfs(res, list, n, k, 1);

return res;
}

private void dfs(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> list, int n, int k, int start) {

if(list.size() == k) {
return;
}

for(int i=start;i<=n;i++) {
if(!list.contains(i))
dfs(res, list, n, k, i+1);
list.remove(list.size()-1);
}
}
}```

381 篇文章35 人订阅

0 条评论

## 相关文章

### 剑指Offer面试题：5.重建二叉树

在二叉树的前序遍历序列中，第一个数字总是树的根结点的值。但在中序遍历序列中，根结点的值在序列的中间，左子树的结点的值位于根结点的值的左边，而右子树的结点的值...

11440

### LintCode 恢复IP地址题目分析

[ "255.255.11.135", "255.255.111.35" ] （顺序无关紧要）

9810

23220

37780

245100

31250

20660

11230

### java获取当前时间和前一天日期

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request....

68950

288100