mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Update 0047.全排列2,添加C#
This commit is contained in:
@ -521,6 +521,38 @@ object Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
### C#
|
||||
```csharp
|
||||
public class Solution
|
||||
{
|
||||
public List<IList<int>> res = new List<IList<int>>();
|
||||
public List<int> path = new List<int>();
|
||||
public IList<IList<int>> PermuteUnique(int[] nums)
|
||||
{
|
||||
Array.Sort(nums);
|
||||
BackTracking(nums, new bool[nums.Length]);
|
||||
return res;
|
||||
}
|
||||
public void BackTracking(int[] nums, bool[] used)
|
||||
{
|
||||
if (nums.Length == path.Count)
|
||||
{
|
||||
res.Add(new List<int>(path));
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < nums.Length; i++)
|
||||
{
|
||||
if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) continue;
|
||||
if (used[i]) continue;
|
||||
path.Add(nums[i]);
|
||||
used[i] = true;
|
||||
BackTracking(nums, used);
|
||||
path.RemoveAt(path.Count - 1);
|
||||
used[i] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
|
Reference in New Issue
Block a user