LeetCode/string/ZigZag.java
// Approach 1: Sort by Row
Approach 1: Sort by Row
Approach 1 : Sort by Row
public String convertSolNoOne(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0; i < Math.min(numRows, s.length()); i++) {
            rows.add(new StringBuilder());
        }
        int curRow = 0;
        boolean goingDown = false;
        for (char c : s.toCharArray()) {
            rows.get(curRow).append(c);
            if (curRow == 0 || curRow == numRows - 1) {
                goingDown = !goingDown;
            }
            curRow += goingDown ? 1 : -1;
        }
        StringBuilder ret = new StringBuilder();
        for (StringBuilder row : rows) {
            ret.append(row);
        }
        return ret.toString();
    }
public String convertSolNoOne ( String s , int numRows ) { if ( numRows == 1 ) { return s ; } List < StringBuilder > rows = new ArrayList < > ( ) ; for ( int i = 0 ; i < Math . min ( numRows , s . length ( ) ) ; i ++ ) { rows . add ( new StringBuilder ( ) ) ; } int curRow = 0 ; boolean goingDown = false ; for ( char c : s . toCharArray ( ) ) { rows . get ( curRow ) . append ( c ) ; if ( curRow == 0 || curRow == numRows - 1 ) { goingDown = ! goingDown ; } curRow += goingDown ? 1 : - 1 ; } StringBuilder ret = new StringBuilder ( ) ; for ( StringBuilder row : rows ) { ret . append ( row ) ; } return ret . toString ( ) ; }
Approach 1: Sort by Row
Approach 1 : Sort by Row
// interesting part on when to switch direction // move up or down // actuall don't need care about space.