-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_0047Permutationsii.java
51 lines (45 loc) · 1.71 KB
/
_0047Permutationsii.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
package com.heatwave.leetcode.problems;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class _0047Permutationsii {
static class Solution {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> temp = new ArrayList<>();
public List<List<Integer>> permuteUnique(int[] nums) {
Arrays.sort(nums);
boolean[] used = new boolean[nums.length];
backtracking(nums, used);
return ans;
}
private void backtracking(int[] nums, boolean[] used) {
if (temp.size() == nums.length) {
// System.out.println("add " + temp + " to ans");
ans.add(new ArrayList<>(temp));
return;
}
for (int i = 0; i < nums.length; i++) {
if (used[i]) {
// System.out.println(i + " used continue");
continue;
}
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
// System.out.println(i + " duplicate continue");
continue;
}
// System.out.println("current temp: " + temp + " add " + nums[i] + " to temp");
temp.add(nums[i]);
used[i] = true;
backtracking(nums, used);
// System.out.println("current temp: " + temp + " remove " + temp.get(temp.size() - 1) + " from temp");
temp.remove(temp.size() - 1);
used[i] = false;
}
}
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {1, 1, 2};
solution.permuteUnique(nums);
}
}