We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e984baa commit 88b2615Copy full SHA for 88b2615
csharp/0047-permutations-ii.cs
@@ -0,0 +1,34 @@
1
+public class Solution
2
+{
3
+ public IList<IList<int>> PermuteUnique(int[] nums)
4
+ {
5
+ Array.Sort(nums);
6
+ var output = new List<IList<int>>();
7
+ Backtrack(output, nums.ToList(), []);
8
+ return output;
9
+ }
10
+
11
+ private void Backtrack(List<IList<int>> output, List<int> nums, List<int> current)
12
13
+ if (nums.Count == 0)
14
15
+ output.Add(new List<int>(current));
16
17
+ else
18
19
+ for (int i = 0; i < nums.Count; i++)
20
21
+ if (i > 0 && nums[i] == nums[i - 1])
22
23
+ continue;
24
25
+ int num = nums[i];
26
+ current.Add(num);
27
+ nums.Remove(num);
28
+ Backtrack(output, nums, current);
29
+ nums.Insert(i, num);
30
+ current.RemoveAt(current.Count - 1);
31
32
33
34
+}
0 commit comments