Day i do not know
This commit is contained in:
11
46. Permutations/46. Permutations.csproj
Normal file
11
46. Permutations/46. Permutations.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_46._Permutations</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
12
46. Permutations/Program.cs
Normal file
12
46. Permutations/Program.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace _46._Permutations;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var arr = new int[] { 1,2,3 };
|
||||
var l = new Solution().Permute(arr);
|
||||
foreach (var row in l)
|
||||
Console.WriteLine(String.Join(" ", row));
|
||||
}
|
||||
}
|
||||
30
46. Permutations/Solution.cs
Normal file
30
46. Permutations/Solution.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace _46._Permutations;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> Permute(int[] nums)
|
||||
{
|
||||
List<IList<int>> l = new List<IList<int>>();
|
||||
Recursion(l, new List<int>(), nums.ToHashSet());
|
||||
return l;
|
||||
}
|
||||
|
||||
private void Recursion(List<IList<int>> result, List<int> cur, HashSet<int> aviable)
|
||||
{
|
||||
if(aviable.Count == 0)
|
||||
{
|
||||
result.Add(cur);
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var av in aviable)
|
||||
{
|
||||
var newAviable = aviable.ToHashSet();
|
||||
newAviable.Remove(av);
|
||||
|
||||
var newCur = cur.ToList();
|
||||
newCur.Add(av);
|
||||
Recursion(result, newCur, newAviable);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user