Home
C# ZigZag Pattern From String Faster
This version runs faster because it CALCULATES the order of the zigzag pattern then applies it to the string
using System;
public class Solution
{
static void Main()
{
Console.WriteLine("Enter String To ZigZag");
string myInput = Console.ReadLine();
Console.WriteLine("Enter The Number of \"Downs\"");
int myDowns = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Convertx(myInput, myDowns));
}
static string Convertx(string s, int numRows)
{
if(numRows < 2)
{
return s;
}
else
{
int sLen = s.Length;
int[] qwe = new int[sLen];
int counter = 0;
for (int rowNum = 0; rowNum < numRows; rowNum++)
{
for (int colNum = 0; colNum < sLen; colNum++)
{
if (
(counter < sLen)
&&
(IsDivisible(colNum + rowNum) || IsDivisible(colNum))
&&
((colNum * 2) + rowNum) < sLen
)
{
qwe[counter] = (colNum * 2) + rowNum;
counter++;
}
}
}
Console.WriteLine(string.Join(",", qwe));
string x = "";
for (int i = 0; i < sLen; i++)
{
x += s[qwe[i]];
}
return x;
bool IsDivisible(int x)
{
if (x % (numRows - 1) == 0)
{
return true;
}
return false;
}
}
}
}
Reader's Comments
Name
Comment
Add a RELEVANT link (not required)
Upload an image (not required)
Uploading...
Home