-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
31 lines (29 loc) · 1.01 KB
/
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
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(nums);
backTrack(nums, new ArrayList<Integer>(), res);
return res;
}
public void backTrack(int[] nums, List<Integer> temp, List<List<Integer>> res) {
int length = nums.length;
if (length == 0) {
res.add(temp);
} else {
int i = 0;
while (i < length) {
int num = nums[i];
int[] tempNums = new int[length - 1];
System.arraycopy(nums, 0, tempNums, 0, i);
System.arraycopy(nums, i + 1, tempNums, i, length - 1 - i);
List<Integer> t = new ArrayList<Integer>(temp);
t.add(num);
backTrack(tempNums, t, res);
while (i + 1 < length && nums[i + 1] == num) {
i++;
}
i++;
}
}
}
}