-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathleetcode_0047_Permutations_II.java
More file actions
33 lines (30 loc) · 1.08 KB
/
leetcode_0047_Permutations_II.java
File metadata and controls
33 lines (30 loc) · 1.08 KB
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
// AC: Runtime: 83 ms, faster than 9.78% of Java online submissions for Permutations II.
// Memory Usage: 40.1 MB, less than 24.22% of Java online submissions for Permutations II.
// backtracking
// T:O(2^n), S:O(n * 2^n)
//
class Solution {
public List<List<Integer>> permuteUnique(int[] nums) {
HashSet<List<Integer>> record = new HashSet<>();
int[] used = new int[nums.length];
backtracking(nums, used, new LinkedList<>(), record);
return new LinkedList<>(record);
}
private void backtracking(int[] nums, int[] used, List<Integer> path, HashSet<List<Integer>> out) {
List<Integer> pathCopy = new LinkedList<>(path);
if (pathCopy.size() >= nums.length) {
out.add(pathCopy);
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i] == 1) {
continue;
}
pathCopy.add(nums[i]);
used[i] = 1;
backtracking(nums, used, pathCopy, out);
pathCopy.remove(pathCopy.size() - 1);
used[i] = 0;
}
}
}