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)

1
2
3
P   A   H   N
A P L S I I G
Y I R

And then read line by line:

1
"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);
1
convert("PAYPALISHIRING", 3)

should return

1
"PAHNAPLSIIGYIR"

Thinking

分成两部分格式化输出= =,这道题有点那啥。


Answer

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

class Solution {

public String convert(String s, int numRows) {

if (numRows == 1)
return s;

// 计算周期
int n = s.length();
int t1 = numRows;
int t2 = numRows - 2;
int k = n / (t1 + t2) + 1;

// 初始化
char[][] c1 = new char[k][t1];
char[][] c2 = new char[k][t2];
for (int i = 0; i < k; i++){
for (int j = 0; j < t1; j++)
c1[i][j] = '_';
for (int j = 0; j < t2; j++)
c2[i][j] = '_';
}

// 分为两个部分
for (int i = 0; i < k; i++) {
int index = i * (t1 + t2);
for (int j = 0; j < t1+t2; j++){
if (index + j < n){
if (j < t1) c1[i][j] = s.charAt(index+j);
else c2[i][t2-1-(j-t1)] = s.charAt(index+j);
}
}
}

// 格式输出
String result = "";
for (int i = 0; i < numRows; i++){
for (int j = 0; j < k; j++){
if (c1[j][i] != '_')
result += c1[j][i];
if (i > 0 && i < numRows-1 && c2[j][i-1] != '_')
result += c2[j][i-1];
}
}

return result;
}

}