class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
//创建一个结果数组
List<List<Integer>> ans = new ArrayList<>();
if(nums.length == 0){
return ans;
}
//不重复问题先排序
Arrays.sort(nums);
//回溯
backtrack(nums,new ArrayList<>(),ans,new boolean[nums.length]);
return ans;
}
public void backtrack(int[] nums,ArrayList<Integer> track,List<List<Integer>> ans,boolean[] visited){
//满足条件
if(track.size() == nums.length){
ans.add(new ArrayList<>(track));
}
//选择
for(int i = 0;i<nums.length;i++){
if(visited[i]) continue;
if(i > 0 && nums[i]==nums[i-1] && visited[i-1]) continue;
track.add(nums[i]);
visited[i] = true;
backtrack(nums,track,ans,visited);
track.remove(track.size()-1);
visited[i] = false;
}
}
}