The string is "PAYPALISHIRING" written with a zigzag pattern on a given number of rows like this: (You could want to display this Pattern in a fixed font for better legibility)
P A H NA p L S i i GY i R
and then read line by Line: "Pahnaplsiigyir"
Write the code that would take a string and make this conversion given a number of rows:
String convert (string text, int nRows);
convert("PAYPALISHIRING", 3) should return "Pahnaplsiigyir" .
The code is as follows:
public class Solution {
public string Convert (string s, int numrows) {
Int[] Aa=new int[s.length ()];
Char[] Sa=new char[s.length ()];
Char[] Ss=s.tochararray ();
int m=0;
if (s.length () <=numrows| | Numrows<=1)
return s;
else{
int n=1;
for (int i=0;i<s.length ();)
{
while (N<=numrows&&i<s.length ())
aa[i++]=n++;
int nn=numrows-1;
while (Nn>0&&i<s.length ())
aa[i++]=nn--;
if (nn==0)
n=2;
}
for (int j=1;j<=numrows;j++)
for (int k=0;k<s.length (); k++)
if (AA[K]==J)
SA[M++]=SS[K];
}
return string.valueof (SA);
}
}
6. ZigZag Conversion