LeetCode: Permutations II

LeetCode: Permutations II

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

/**
* Description: Given a collection of numbers that might contain duplicates, return all possible unique permutations.
* <p/>
* For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].
*
* @author hzhou
*/
public class PermutationsII {
public List<List<Integer>> permuteUnique(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0) {
return result;
}
Arrays.sort(nums);
boolean[] visited = new boolean[nums.length];
helper(nums, result, new ArrayList<Integer>(), visited);
return result;
}
private void helper(int[] nums, List<List<Integer>> result, List<Integer> crt, boolean[] visited) {
if (crt.size() >= nums.length) {
result.add(new ArrayList<Integer>(crt));
return;
}
for (int i = 0; i < nums.length; i++) {
if (visited[i] || (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1])) {
continue;
}
crt.add(nums[i]);
visited[i] = true;
helper(nums, result, crt, visited);
visited[i] = false;
crt.remove(crt.size() - 1);
}
}
}