New tasks
This commit is contained in:
11
6. Zigzag Conversion/6. Zigzag Conversion.csproj
Normal file
11
6. Zigzag Conversion/6. Zigzag Conversion.csproj
Normal 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>
|
||||
9
6. Zigzag Conversion/Program.cs
Normal file
9
6. Zigzag Conversion/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _6._Zigzag_Conversion;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(new Solution().Convert("PAYPALISHIRING", 3));
|
||||
}
|
||||
}
|
||||
41
6. Zigzag Conversion/Solution.cs
Normal file
41
6. Zigzag Conversion/Solution.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user