题目链接:
https://leetcode.com/problems/zigzag-conversion/description/
题意理解
要求把原来的字符按竖着搞一个“之”字型拜访,然后横着拼接起来。
我的代码
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
char[] cs = s.toCharArray();
int len = cs.length;
char[][] zigzags = new char[numRows][len];
int x = 0;
int y = 0;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < len; j++) {
zigzags[i][j] = ' ';
}
}
// zigzags[x][y]
for (int i = 0; i < len; i++) {
zigzags[x][y] = cs[i];
if (y % (numRows - 1) == 0) {
if (x != numRows - 1) {
x++;
} else {
y++;
x--;
}
} else {
y++;
x--;
}
}
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < len; j++) {
if (zigzags[i][j] != ' ') {
sb.append(zigzags[i][j]);
}
}
}
return sb.toString();
}
}