题目
思路
- 与全排列类似,只是关键在于去重,但是其思想与组合总和 II的去重思想一致
-
代码
public List<List<Integer>> permuteUnique(int[] nums) {List<List<Integer>> res = new ArrayList<>();Arrays.sort(nums);dfs(nums, 0, res, new ArrayList<>());return res;}public void dfs(int[] nums, int index, List<List<Integer>> res, List<Integer> tmp) {if (nums.length == tmp.size()) {res.add(new ArrayList<>(tmp));return;}for (int i = index; i < nums.length; i++) {//去重if (nums[i] == -100 || i != index && nums[i] == nums[i - 1]) continue;tmp.add(nums[i]);nums[i] = -100;dfs(nums, index, res, tmp);nums[i] = tmp.remove(tmp.size() - 1);}}
