一:题目
二:上码
class Solution {
private List<List<Integer>> ans = new ArrayList<>();
private List<Integer> path = new ArrayList<>();
private boolean[] used;
public void getAns(int[] nums,boolean[] used) {
if (path.size() == nums.length) {
ans.add(new ArrayList<>(path));
}
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i-1] && used[i-1] == false) continue;
if (used[i] == false) {
used[i] = true;
path.add(nums[i]);
getAns(nums,used);
path.remove(path.size()-1);
used[i] = false;
}
}
}
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
used = new boolean[nums.length];
getAns(nums,used);
return ans;
}
}
|