acwing-92. 递归实现指数型枚举

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
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();
dfs(1, n, new ArrayList<>());
System.out.println("");
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, List<Integer> ans){

if(ans.size() > 0 && !res.contains(ans)){
res.add(new ArrayList<Integer>(ans));
}

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