import java.util.*;

public class Solution { ArrayList path = new ArrayList<>(); ArrayList<ArrayList> res = new ArrayList<>(); public ArrayList<ArrayList> permute(int[] num) { int[] used = new int[num.length]; Arrays.sort(num); back(num,used); return res; }

public void back(int[] nums,int[] used){
    if(path.size() == nums.length){
        res.add(new ArrayList<>(path));
    }
    for(int i = 0;i < nums.length;i++){
        if(i > 0 && nums[i] == nums[i-1] && used[i] == 0){
            continue;
        }
        if(used[i]==0){ 
            path.add(nums[i]);
            used[i] = 1;
            back(nums,used);
            used[i] = 0;
            path.remove(path.size() - 1);
        }
    }
}
   

}