Day4
This commit is contained in:
11
118. Pascal's Triangle/118. Pascal's Triangle.csproj
Normal file
11
118. Pascal's Triangle/118. Pascal's Triangle.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_118._Pascal_s_Triangle</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
12
118. Pascal's Triangle/Program.cs
Normal file
12
118. Pascal's Triangle/Program.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace _118._Pascal_s_Triangle
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var res = new Solution().Generate(300);
|
||||
foreach(var r in res)
|
||||
Console.WriteLine(String.Join(" ", r));
|
||||
}
|
||||
}
|
||||
}
|
||||
26
118. Pascal's Triangle/Solution.cs
Normal file
26
118. Pascal's Triangle/Solution.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace _118._Pascal_s_Triangle;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> Generate(int numRows)
|
||||
{
|
||||
List<IList<int>> triangle = new List<IList<int>>() { new List<int> { 1 } };
|
||||
if(numRows > 1)
|
||||
triangle.Add(new List<int>() { 1,1 });
|
||||
numRows -= 2;
|
||||
while (numRows-- > 0)
|
||||
{
|
||||
IList<int> prev = triangle[^1];
|
||||
int nextCount = prev.Count;
|
||||
List<int> row = new List<int>() { 1 };
|
||||
for(int i = 1; i < nextCount; i++)
|
||||
{
|
||||
row.Add(prev[i-1] + prev[i]);
|
||||
}
|
||||
row.Add(1);
|
||||
triangle.Add(row);
|
||||
}
|
||||
|
||||
return triangle;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user