public class Solution {
public string Convert(string s, int numRows) {
int n = s.Length, r = numRows;
if (r == 1 || r >= n) {
return s;
}
int t = r * 2 - 2;
int c = (n + t - 1) / t * (r - 1);
char[][] mat = new char[r][];
for (int i = 0; i < r; ++i) {
mat[i] = new char[c];
}
for (int i = 0, x = 0, y = 0; i < n; ++i) {
mat[x][y] = s[i];
if (i % t < r - 1) {
++x; // 向下移动
} else {
--x;
++y; // 向右上移动
}
}
StringBuilder ans = new StringBuilder();
foreach (char[] row in mat) {
foreach (char ch in row) {
if (ch != 0) {
ans.Append(ch);
}
}
}
return ans.ToString();
}
}