-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-77-Combinations.java
47 lines (40 loc) · 1.26 KB
/
LeetCode-77-Combinations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
LeetCode: https://leetcode.com/problems/combinations/
LintCode: http://www.lintcode.com/problem/combinations/
JiuZhang: http://www.jiuzhang.com/solutions/combinations/
ProgramCreek: http://www.programcreek.com/2014/03/leetcode-combinations-java/
Analysis:
DFS
*/
public class Solution {
// 1.DFS(recursive)
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> set = new ArrayList<Integer>();
if(n <= 0 || n < k) return result;
if(n == k){
for(int i = 0; i < n; i++){
set.add(i + 1);
}
result.add(set);
return result;
}
int start = 0;
combine(n, k, start, result, set);
return result;
}
private void combine(int n, int k, int start, List<List<Integer>> result, List<Integer> set){
if(set.size() == k){
result.add(new ArrayList<Integer>(set));
return;
}
for(int i = start; i < n; i++){
if(set.contains(i + 1)){
continue;
}
set.add(i + 1);
combine(n, k, i + 1, result, set);
set.remove(set.size() - 1);
}
}
}