Day i do not know
This commit is contained in:
11
77. Combinations/77. Combinations.csproj
Normal file
11
77. Combinations/77. Combinations.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_77._Combinations</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
11
77. Combinations/Program.cs
Normal file
11
77. Combinations/Program.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace _77._Combinations;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var res = new Solution().Combine(4, 2);
|
||||
foreach(var row in res)
|
||||
Console.WriteLine(string.Join(" ", row));
|
||||
}
|
||||
}
|
||||
26
77. Combinations/Solution.cs
Normal file
26
77. Combinations/Solution.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace _77._Combinations;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> Combine(int n, int k)
|
||||
{
|
||||
List<IList<int>> res = new List<IList<int>>();
|
||||
Recursion(res, new List<int>(), 1, n, k);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void Recursion(List<IList<int>> result, List<int> cur, int minNum, int maxNum, int count)
|
||||
{
|
||||
if (cur.Count == count)
|
||||
{
|
||||
result.Add(cur.ToList());
|
||||
return;
|
||||
}
|
||||
for (int toAdd = minNum; toAdd <= maxNum; toAdd++)
|
||||
{
|
||||
cur.Add(toAdd);
|
||||
Recursion(result, cur, toAdd + 1, maxNum, count);
|
||||
cur.RemoveAt(cur.Count-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user