-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0047.java
28 lines (25 loc) · 799 Bytes
/
0047.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 {
private List<List<Integer>> res = new ArrayList<>();
private boolean[] st;
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
st = new boolean[nums.length];
dfs(new ArrayList<>(), nums);
return res;
}
private void dfs(List<Integer> path, int[] nums) {
if (path.size() == nums.length) {
res.add(new ArrayList<>(path));
return;
}
for (int i = 0; i < nums.length; i++) {
if (st[i]) continue;
if (i > 0 && nums[i] == nums[i - 1] && !st[i - 1]) continue;
st[i] = true;
path.add(nums[i]);
dfs(path, nums);
path.remove(path.size() - 1);
st[i] = false;
}
}
}