把握计数变量的循环即可,解决代码如下:
public class Solution {
public String convert(String s, int numRows) {
if (numRows == 1 || s.length() <= 1) {
return s;
}
String[] strings = new String[numRows];
for (int i = 0; i < numRows; i++) {
strings[i] = "";
}
int n = s.length();
int j = 0;
String result = "";
boolean add = true;
for (int i = 0; i < n; i++) {
strings[j] += s.charAt(i);
if (j == numRows - 1) {
add = false;
}
if (add) {
j++;
}else{
j--;
}
if (j == 0 && !add) {
add = true;
}
}
for (String string : strings) {
result += string;
System.out.println(string);
}
return result;
}
}