Sponsored
Sponsored
This approach uses backtracking to generate permutations and a boolean array to track used elements in the current permutation combination. By sorting the array at the start, we can easily skip duplicates.
Time Complexity: O(n * n!) due to the generation of all permutations.
Space Complexity: O(n!) for storing the permutations and O(n) for the recursion stack.
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.List;
4
5public class PermutationsUnique {
6 public List<List<Integer>> permuteUnique(int[] nums) {
7 List<List<Integer>> results = new ArrayList<>();
8 Arrays.sort(nums);
9 backtrack(nums, new boolean[nums.length], new ArrayList<>(), results);
10 return results;
11 }
12
13 private void backtrack(int[] nums, boolean[] used, List<Integer> current, List<List<Integer>> results) {
14 if (current.size() == nums.length) {
15 results.add(new ArrayList<>(current));
16 return;
17 }
18 for (int i = 0; i < nums.length; i++) {
19 if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) {
20 continue;
21 }
22 used[i] = true;
23 current.add(nums[i]);
24 backtrack(nums, used, current, results);
25 used[i] = false;
26 current.remove(current.size() - 1);
27 }
28 }
29
30 public static void main(String[] args) {
31 PermutationsUnique pu = new PermutationsUnique();
32 System.out.println(pu.permuteUnique(new int[]{1, 1, 2}));
33 }
34}
The Java solution implements a similar logic to the Python solution using arrays and lists for dynamic use checks and to form the permutation combinations.
This method carries out permutations by recursively swapping elements and employs a set to track unique permutations and avoid generating duplicate results. It does not require sorting initially but uses swaps directly to generate permutations.
Time Complexity: O(n * n!)
Space Complexity: O(n * n!) because of the set used to store permutations.
1using System;
2using System.Collections.Generic;
3
4class PermutationsUniqueSwapSet {
public IList<IList<int>> PermuteUnique(int[] nums) {
var results = new HashSet<string>();
Permute(nums, 0, results);
var listResults = new List<IList<int>>();
foreach(var res in results) {
var lst = new List<int>();
foreach(var c in res.Split(',')) {
lst.Add(int.Parse(c));
}
listResults.Add(lst);
}
return listResults;
}
private void Permute(int[] nums, int start, HashSet<string> results) {
if (start == nums.Length) {
results.Add(string.Join(",", nums));
return;
}
for (int i = start; i < nums.Length; i++) {
Swap(nums, start, i);
Permute(nums, start + 1, results);
Swap(nums, start, i);
}
}
private void Swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
static void Main() {
var solution = new PermutationsUniqueSwapSet();
var result = solution.PermuteUnique(new[] { 1, 1, 2 });
foreach (var perm in result) {
Console.WriteLine(string.Join(",", perm));
}
}
}
The C# solution embraces swapping while using a HashSet to collect unique permutations by leveraging string representations of the permutations.