java:
class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
boolean[] used = new boolean[nums.length];
Arrays.sort(nums);//注!!有重复一定要排序
backTracking(nums, used);
return res;
}
public void backTracking(int[] nums, boolean[] used) {
if (path.size() == nums.length){
res.add(new ArrayList<>(path));
return ;
}
for (int i = 0; i < nums.length; i++) {
if (i != 0 && used[i - 1] == false && nums[i] == nums[i - 1])
continue;//注!!跳过层相同的
if (used[i] != true) {//注!!跳过树枝使用过的数字
path.add(nums[i]);
used[i] = true;
backTracking(nums, used);
path.remove(path.size() - 1);
used[i] = false;
}
}
}
}