Skip to content

Commit 88e558a

Browse files
authored
Merge pull request #3651 from NotADucc/0047
create 0047-permutations-ii.cs
2 parents d614a00 + 88b2615 commit 88e558a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: csharp/0047-permutations-ii.cs

+34
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)