The string "Paypalishiring" is written with a zigzag pattern on a given number of rows like this: (You could want to display T His pattern in a fixed font for better legibility)
P A H N
A P L S i i G
Y I R
And then read 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".
Public classSolution { PublicString Convert (String s,intnumrows) { intPSTR = 0; String ret= ""; string[] Graph=NewString[numrows]; for(inti = 0; i < numrows; i++) {Graph[i]= ""; } while(PStr <s.length ()) { for(inti = 0; i < numrows; i++) {Graph[i]+ = S.charat (pstr++); if(PStr >= s.length ()) Break; } if(PStr >= s.length ()) Break; for(inti = numRows-2; i > 0; i--) {//even column, 2 element shorter than the odd column, fill from bottom to topGraph[i] + = S.charat (pstr++); if(PStr >= s.length ()) Break; } } for(inti = 0; i < numrows; i++) {ret+=Graph[i]; } returnret; }}
6. ZigZag Conversion