Given a list of numbers with duplicate number in it. Find all unique permutations.
Example
For numbers[1,2,2]the unique permutations are:
[
[1,2,2],
[2,1,2],
[2,2,1]
]
Note
和没有重复元素的 Permutation 一题相比,只加了两句话:
Arrays.sort(nums) // 排序这样所有重复的数
if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) 跳过会造成重复的情况
Code
publicclassSolution {/* * @param nums: A list of integers. * @return: A list of permutations. */publicList<List<Integer>> permuteUnique(int[] nums) {// write your code hereList<List<Integer>> res =newArrayList<>();if (nums ==null) {return res; }Arrays.sort(nums);helper(nums,newboolean[nums.length],newArrayList<Integer>(), res);return res; }privatevoidhelper(int[] nums,boolean[] visited,List<Integer> list,List<List<Integer>> res) {if (nums.length==list.size()) {res.add(newArrayList<Integer>(list));return; } for (int i =0; i <nums.length; i++) {if (visited[i]) {continue; }//curr one the same as if (i >0&& nums[i] == nums[i -1] &&!visited[i -1]) {continue; }list.add(nums[i]); visited[i] =true;helper(nums, visited, list, res);list.remove(list.size() -1); visited[i] =false; } }}