-
Notifications
You must be signed in to change notification settings - Fork 0
/
77.组合.java
47 lines (40 loc) · 1.22 KB
/
77.组合.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
import java.util.ArrayList;
import java.util.List;
/*
* @lc app=leetcode.cn id=77 lang=java
*
* [77] 组合
*/
class Solution {
private int n = 0;
private int k = 0;
public List<List<Integer>> combine(int n, int k) {
this.n = n;
this.k = k;
List<Integer> target = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
for(int i = 1; i <= n ; i++){
target.add(i);
}
helper(result, new ArrayList(), target);
return result;
}
private void helper(List<List<Integer>> result, List<Integer> selected, List<Integer> target){
if(target.size() == 0 && selected.size() < k){
return;
}else if(selected.size() == k){
result.add(selected);
return;
}
for(int i = 0; i < target.size(); i++){
List<Integer> tempSelect = new ArrayList<>();
tempSelect.addAll(selected);
tempSelect.add(target.get(i));
List<Integer> tempTarget = new ArrayList<>();
for(int j = i+1; j < target.size(); j++){
tempTarget.add(target.get(j));
}
helper(result, tempSelect, tempTarget);
}
}
}