6. ZigZag Conversion

Problem

The string "PAYPALISHIRING" is written in 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   N
A P L S I I G
Y   I   R

And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows:

string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

Related Topics:

String

Analysis

  [ 0]            [ 6]            [12]          
  [ 1]      [ 5]  [ 7]      [11]  [13]    
  [ 2]  [ 4]      [ 8]  [10]      [14]
  [ 3]            [ 9]            [15]

可以发现:

  • 每一完整的列,其差值为 2 * numRows - 2

  • 斜线左右分割这差值,其上下层之间,分割的差值 ±2

Code

Last updated