public class Solution {
public String convert(String s, int numRows) {
if (numRows == 1 || s.length() <= numRows) {
return s;
}
StringBuilder sb = new StringBuilder();
int diff = (numRows - 1) * 2;
for (int i = 0; i < numRows; i++) {
if (i == 0 || i == numRows - 1) {
for (int j = i; j < s.length(); j += diff) {
sb.append(s.charAt(j));
}
} else {
int j = i;
boolean condition = true;
int step1 = (numRows - i - 1) * 2;
int step2 = diff - step1;
while (j < s.length()) {
sb.append(s.charAt(j));
if (condition) {
j += step1;
} else {
j += step2;
}
condition = !condition;
}
}
}
return sb.toString();
}
}