import java.util.*;
public class Solution { public ArrayList<ArrayList> res = new ArrayList<>(); public ArrayList<ArrayList> permuteUnique(int[] num) { int n = num.length; if(n == 0 || num == null) return res; Arrays.sort(num); ArrayList list = new ArrayList<>(); dfs(num,list,new boolean[num.length]); return res; } public void dfs(int[] num,ArrayList list,boolean[] f) { if(list.size() == num.length) { res.add(new ArrayList<>(list)); return; } for(int i = 0; i < num.length; i++) { if(f[i]) continue; if(i > 0 && num[i] == num[i - 1] && !f[i - 1]) continue; f[i] = true; list.add(num[i]); dfs(num,list,f); list.remove(list.size() - 1); f[i] = false; } } }