Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public String convert(String s, int numRows) {
- if (numRows == 1) return s;
- List<StringBuilder> rows = new ArrayList<>();
- int currentRow = 0;
- boolean direction = false;
- for (char c : s.toCharArray()) {
- if (rows.size() == currentRow) {
- rows.add(new StringBuilder());
- }
- rows.get(currentRow).append(c);
- if (currentRow == 0 || currentRow == numRows - 1) {
- direction = !direction;
- }
- currentRow += direction ? 1 : -1;
- }
- StringBuilder result = new StringBuilder();
- for (StringBuilder row : rows) {
- result.append(row);
- }
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement