mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-13 22:35:09 +08:00
Update0046.全排列,添加C#
This commit is contained in:
@ -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">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
|
Reference in New Issue
Block a user