forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutations II.java
28 lines (24 loc) · 884 Bytes
/
Permutations II.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
boolean used[] = new boolean[nums.length];
permutationsFinder(nums,ans,new ArrayList<>(),used);
return ans;
}
static void permutationsFinder(int[] nums,List<List<Integer>> ans,List<Integer> list,boolean used[]){
if(list.size() == nums.length){
ans.add(new ArrayList<>(list));
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;
list.add(nums[i]);
used[i] = true;
permutationsFinder(nums,ans,list,used);
list.remove(list.size()-1);
used[i] = false;
}
}
}