题目链接
回溯
class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();List<Integer> list = new ArrayList<Integer>();boolean[] used;public List<List<Integer>> permuteUnique(int[] nums) {used = new boolean[nums.length];Arrays.fill(used,false);// 给定一个可包含重复数字的序列,要返回所有不重复的全排列,去重一定要对元素进行排序Arrays.sort(nums);backtracking(nums, used);return res;}public void backtracking(int[] nums, boolean[] used){if(list.size() == nums.length){res.add(new ArrayList<Integer>(list));return;}for(int i = 0; i < nums.length; i++){if(used[i] || i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false){continue;}used[i] = true;list.add(nums[i]);backtracking(nums,used);used[i] = false;list.removeLast();}}
}