๐ 47. ๅ จๆๅ II
2022ๅนด6ๆ9ๆฅ
- algorithm
๐ 47. ๅ จๆๅ II
้พๅบฆ: ๐
้ฎ้ขๆ่ฟฐ
่งฃๆณ
class Solution {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
// ๆ่ทฏ๏ผ
// ้ๅคๆฐ็ป & ๅ
จๆๅ
Arrays.sort(nums); // ็จไบๅๅฑๅป้
int len = nums.length;
LinkedList<Integer> path = new LinkedList<>();
int[] used = new int[len];
mySol(nums, len, used, path);
return res;
}
private void mySol(int[] nums, int len, int[] used, LinkedList<Integer> path) {
// ้ๅฝ็ปๆญขๆกไปถ
if(path.size() == len) {
res.add(new LinkedList<>(path));
return;
}
for(int i = 0; i < len; i ++) {
if(used[i] == 1) {
continue;
}
if(i > 0 && nums[i] == nums[i - 1] && used[i - 1] == 0) {
continue; // ๅๅฑๅป้๏ผ็ปๅพ็่งฃ
}
path.addLast(nums[i]);
used[i] = 1;
mySol(nums, len, used, path);
used[i] = 0;
path.removeLast();
}
}
}