一、问题描述
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 RAnd 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"
.
二、思路分析
由题可以看出,第一行每相邻两个字符间的间隔为2*rows-2;每个间隔里除首行和末行外其余的行都还要加个字符,该字符距离该间隔起始字符的位置为2*rows-2-2*i,其中i为当前行数。
三、代码