Update0046.全排列,添加C#

This commit is contained in:
eeee0717
2023-12-21 09:36:09 +08:00
parent f9342095c9
commit eff7a7a9b2

View File

@ -486,6 +486,37 @@ object Solution {
}
}
```
### C#
```csharp
public class Solution
{
public IList<IList<int>> res = new List<IList<int>>();
public IList<int> path = new List<int>();
public IList<IList<int>> Permute(int[] nums)
{
var used = new bool[nums.Length];
BackTracking(nums, used);
return res;
}
public void BackTracking(int[] nums, bool[] used)
{
if (path.Count == nums.Length)
{
res.Add(new List<int>(path));
return;
}
for (int i = 0; i < nums.Length; i++)
{
if (used[i]) continue;
used[i] = true;
path.Add(nums[i]);
BackTracking(nums, used);
used[i] = false;
path.RemoveAt(path.Count - 1);
}
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">