题目链接
1. 题目考点
- 回溯剪枝
2. 考点解析
public class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
Stack<Integer> path = new Stack<>();
dfs(nums, used, path);
return res;
}
private void dfs(int[] nums, boolean[] used, Stack<Integer> path) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; ++i) {
if (used[i]) continue;
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])
continue;
path.push(nums[i]);
used[i] = true;
dfs(nums, used, path);
used[i] = false;
path.pop();
}
}
}