| 33 | // } |
| 34 | |
| 35 | public String convert(String s, int numRows) { |
| 36 | if (numRows <= 1) return s; |
| 37 | int len = s.length(); |
| 38 | char[] chars = s.toCharArray(); |
| 39 | StringBuilder[] sbs = new StringBuilder[numRows]; |
| 40 | for (int i = 0; i < numRows; i++) { |
| 41 | sbs[i] = new StringBuilder(); |
| 42 | } |
| 43 | int i = 0; |
| 44 | while (i < len) { |
| 45 | for (int j = 0; j < numRows && i < len; ++j) { |
| 46 | sbs[j].append(chars[i++]); |
| 47 | } |
| 48 | for (int j = numRows - 2; j >= 1 && i < len; --j) { |
| 49 | sbs[j].append(chars[i++]); |
| 50 | } |
| 51 | } |
| 52 | for (i = 1; i < numRows; i++) { |
| 53 | sbs[0].append(sbs[i]); |
| 54 | } |
| 55 | return sbs[0].toString(); |
| 56 | } |
| 57 | |
| 58 | public static void main(String[] args) { |
| 59 | Solution solution = new Solution(); |