-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
38 lines (35 loc) · 863 Bytes
/
Solution.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
29
30
31
32
33
34
35
36
37
38
/**
1,1,2
1 2
/ \ |
1 2 1
| | |
2 1 1
*/
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
if (nums.length == 0) {
return new ArrayList<>();
}
Arrays.sort(nums);
ArrayList<Integer> tmp = new ArrayList<>();
for (int num : nums) {
tmp.add(num);
}
List<List<Integer>> res = new ArrayList<>();
perm(new ArrayList<>(), tmp, res);
return res;
}
private void perm(final ArrayList<Integer> c, final ArrayList<Integer> n, List<List<Integer>> res) {
if (n.size() == 0) { res.add(c); return; }
for (int i = 0; i < n.size(); i++) {
if (i == 0 || n.get(i - 1) != n.get(i)) {
ArrayList<Integer> ctmp = (ArrayList<Integer>) c.clone();
ArrayList<Integer> ntmp = (ArrayList<Integer>) n.clone();
ctmp.add(ntmp.get(i));
ntmp.remove(i);
perm(ctmp, ntmp, res);
}
}
}
}