Title Requirements:
The string is "PAYPALISHIRING" written with a zigzag pattern on a given number of rows like this: (You may 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 "
use coordinate transformation to do this problem. We now consider the nrows=4, in fact, for each row, if you do not consider the middle column, only the full-full column, then the next coordinate is the previous base on the basis of plus (NROWS-1), such as 0->6->12,1->7->13,2-> 8,3->9, therefore, for the i=0 line and the I=nrows line, directly in accordance with such a coordinate conversion read into, and for the middle row, each time according to such a law read into a character, you need to append a character, 1->5,2->4. What is the law of this coordinate transformation, for 0->6, is a nrows=4 zigzag, similarly, for < Span style= "font-size:18px" >1->5,2->4, respectively < Font-family:monospace of span style= "font-size:18px" >nrows=3, , that is, line I, the law of increasing coordinates increases 2* (nrows-1-i) on the basis of the previous coordinate.
完整代码如下:
Leetcode6 Zigzag Conversion