acwing-93. 递归实现组合型枚举

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
import java.util.*;
public class Main{
static List<List<Integer>> res = new ArrayList<>();
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();

dfs(1, n, m, new ArrayList<>());

for(List<Integer> list : res){
for(int i = 0; i < list.size(); i++){
System.out.print(list.get(i));
if(i != list.size()-1){
System.out.print(" ");
}
}
System.out.println("");
}
}

public static void dfs(int cur, int n, int m, List<Integer> ans){
if(ans.size() == m && !res.contains(ans)){
res.add(new ArrayList<>(ans));
return;
}

for(int i = cur; i <= n; i++){
ans.add(i);
dfs(i+1, n, m, ans);
ans.remove(ans.size()-1);
}
}
}