回溯
public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> result = new ArrayList<>(); boolean[] visited = new boolean[nums.length]; Arrays.sort(nums); backtrack(result,nums,new ArrayList<Integer>(),visited); return result; } private void backtrack(List<List<Integer>> result, int[] nums, ArrayList<Integer> temp, boolean[] visited) { if(temp.size() == nums.length){ result.add(new ArrayList<>(temp)); return; } for(int i = 0;i<nums.length;i++){ if(visited[i]) continue; if(i>0 && !visited[i-1] && nums[i] == nums[i-1]) continue; visited[i] = true; temp.add(nums[i]); backtrack(result, nums, temp, visited); visited[i] = false; temp.remove(temp.size()-1); } }
依然是第二遍,依然不会做。。。
——2020.7.30