public class permutation {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4};
int len = arr.length;
boolean f[] = new boolean[len];
List<Integer> list = new ArrayList<>();
dfs(arr, len, 0, f, list);
}
private static void dfs(int[] arr, int len, int depth, boolean[] f, List<Integer> list){
if(len == depth){
System.out.println(list);
}else{
for(int i = 0;i < len;i++){
if (f[i]) continue;
list.add(arr[i]);
f[i] = true;
dfs(arr, len, depth + 1, f, list);
f[i] = false;
list.remove(list.size() - 1);
}
}
}
}
|