AcWing 94. 【Java】递归实现排列型枚举
原题链接
简单
作者:
tt2767
,
2019-12-11 00:04:10
,
所有人可见
,
阅读 815
import java.util.*;
import java.util.stream.*;
public class Main{
void run(){
int n = jin.nextInt();
dfs(n, 0);
}
void dfs(int n, int status){
if (buffer.size() == n){
String res = String.join(" ", buffer.stream().map(x->String.valueOf(x+1)).collect(Collectors.toList()));
System.out.println(res);
} else {
for (int i = 0 ; i < n ;i ++){
if (((status >> i ) & 1) == 0){
buffer.add(i);
dfs(n, status | ( 1 << i));
buffer.remove(buffer.size()-1);
}
}
}
}
private List<Integer> buffer = new ArrayList<Integer>();
private Scanner jin = new Scanner(System.in);
public static void main(String[] args) throws Exception {new Main().run();}
}