-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM56.java
More file actions
36 lines (32 loc) · 975 Bytes
/
BM56.java
File metadata and controls
36 lines (32 loc) · 975 Bytes
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
package NiukeTOP101;
import java.util.ArrayList;
import java.util.Arrays;
public class BM56 {
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
ArrayList<Integer> ans = new ArrayList<>();
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
Arrays.sort(num);
boolean[] numState = new boolean[num.length];
dfs(num, numState);
return ret;
}
private void dfs(int[] num, boolean[] numState) {
if(ans.size() == num.length){
ret.add(new ArrayList<>(ans));
return;
}
for (int i = 0; i < num.length; i++) {
if(numState[i]){
continue;
}
if(i >= 1 && num[i] == num[i-1] && numState[i-1]){
continue;
}
ans.add(num[i]);
numState[i] = true;
dfs(num, numState);
numState[i] = false;
ans.remove(ans.size() - 1);
}
}
}