-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermutationsII.java
65 lines (56 loc) · 1.47 KB
/
PermutationsII.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// https://leetcode.com/problems/permutations-ii/
// #backtracking
class Solution {
// fix the each element at the first position => append the remaining element
// 1, 2, 3
/*
start from 1:
1-2-3
1-3-2
start from 2:
2-1-3
2-3-1
start from 3:
3-1-2
3-2-1
*/
public List<List<Integer>> permuteUnique(int[] nums) {
// store frequency of each element
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
int frequency = 1;
if (map.containsKey(num)) {
frequency += map.get(num);
}
map.put(num, frequency);
}
LinkedList<Integer> permutation = new LinkedList<>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
backtracking(result, permutation, map, nums.length);
return result;
}
private void backtracking(
List<List<Integer>> result,
LinkedList<Integer> permutation,
HashMap<Integer, Integer> map,
int limit) {
if (permutation.size() == limit) {
result.add(new ArrayList<Integer>(permutation));
return;
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
int num = entry.getKey();
int frequency = entry.getValue();
if (frequency == 0) {
continue;
}
// forward
permutation.addLast(num);
map.put(num, frequency - 1);
backtracking(result, permutation, map, limit);
// backward
permutation.removeLast();
map.put(num, frequency);
}
}
}