1.题目
2.解法
①二维数组+dfs
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
boolean[] visited = new boolean[nums.length];
int n = nums.length;
Arrays.sort(nums);
dfs(n, nums, visited, res, temp);
return res;
}
private void dfs(int n, int[] nums, boolean[] visited, List<List<Integer>> res, List<Integer> temp){
if(temp.size() == n){
res.add(new ArrayList<>(temp));
return;
}
for(int i = 0; i < n; i++){
if(i != 0 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
if(visited[i]) continue;
temp.add(nums[i]);
visited[i] = true;
dfs(n, nums, visited, res, temp);
temp.remove(temp.size() - 1);
visited[i] = false;
}
}
}