题目
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)
1
2
3 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:
1 string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
解答
1 | public String convert(String s, int nRows){ |
注解
一般性的找规律题。
按照生成的顺序,发现首行和末行都是以2 * nRows - 2增加的。
中间nRow - 2行,在2 * nRow - 2步数增加的情况下,加入2 * nRows - 2 - 2 * (nRow - 2)小步数增加。
