感觉中间用数组方便些 最后用stream来转化
用时也少一些
class Solution {
static int[] root;
static HashSet<List<Integer>> res = new HashSet<>();
public static List<List<Integer>> permutation(int[] nums) {
root = nums;
boolean[] tmp = new boolean[root.length];
dfs(new int[root.length],0,tmp);
return res.stream().collect(Collectors.toList());
}
public static void dfs(int[] a,int id,boolean[] used){
if (id == root.length) {
res.add(Arrays.stream(a).boxed().collect(Collectors.toList()));
}
for (int i = 0; i < root.length; ++i){
if (!used[i]){
used[i] = true;
a[id] = root[i];
dfs(a,id+1,used);
used[i] = false;
}
}
}
}