每日一题:zigzag

题目

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 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:

1
string (string text, int nRows);

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

解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public String convert(String s, int nRows){
if(nRows == 1){
return s;
}

StringBuffer sb = new StringBuffer();
int step = 2 * nRows - 2;
int length = s.length();

for(int i = 0; i < length; i += step){
sb.append(s.charAt(i));
}

for(int i = 1; i < nRows - 1; i++){
for(int j = i; j < length; j+= step){
sb.append(s.charAt(j));
if(j + step - 2 * i < length){
sb.append(s.charAt(j + step - 2 * i));
}
}
}

for(int i = nRows - 1; i < length; i += step){
sb.append(s.charAt(i));
}

return sb.toString();
}

注解

一般性的找规律题。

按照生成的顺序,发现首行和末行都是以2 * nRows - 2增加的。

中间nRow - 2行,在2 * nRow - 2步数增加的情况下,加入2 * nRows - 2 - 2 * (nRow - 2)小步数增加。