New tasks

This commit is contained in:
nullptroma
2023-03-05 00:46:41 +03:00
parent 1288b304ae
commit b285770a9c
15 changed files with 1018 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>_6._Zigzag_Conversion</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
namespace _6._Zigzag_Conversion;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Solution().Convert("PAYPALISHIRING", 3));
}
}

View File

@@ -0,0 +1,41 @@
using System.Text;
namespace _6._Zigzag_Conversion;
public class Solution {
public string Convert(string s, int numRows)
{
if (numRows == 1)
return s;
int stepDown = (numRows-2) * 2 + (numRows>0 ? 1 : 0);
int stepTop = 0;
StringBuilder sb = new StringBuilder();
for (int start = 0; start < numRows; start++)
{
int j = start;
bool toDown = true;
while (j<s.Length)
{
sb.Append(s[j]);
if (stepDown == 0)
j += stepTop;
else if (stepTop == 0)
j += stepDown;
else if (toDown)
j += stepDown;
else
j += stepTop;
j++;
toDown = !toDown;
}
stepDown -= 2;
if (stepDown < 0)
stepDown = 0;
if (stepTop == 0)
stepTop = 1;
else
stepTop += 2;
}
return sb.ToString();
}
}