- Question Description
- My Key
package LeetCode;
public class Test {
public static void main(String[] args) {
System.out.println(convert("PAYPALISHIRING", 3));
}
public static String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
StringBuilder ret = new StringBuilder();
int len = s.length();
int cycleLen = 2*numRows - 2;
int j = 0;
for (int i = 0; i < numRows; i++) {
if (i % (numRows -1) == 0) {
for (j = 0; (i+j) < len; j += cycleLen)
ret.append(s.charAt(i+j));
} else {
for (j = 0; j < len ; j += cycleLen) {
if ((i+j) < len)
ret.append(s.charAt(i+j));
if ((cycleLen-i+j)<len)
ret.append(s.charAt(cycleLen-i+j));
}
}
}
return ret.toString();
}
}