AdSense

Saturday, April 30, 2016

Zigzag conversion, way II

There was a common way I posted last year. Now I realize there is an easier way: uses an array of string builder, add characters in the zigzag way, and combine them all together.


public String convert(String s, int numRows) {
        if (s == null || s.length() <= 1 || s.length() <= numRows || numRows == 1)
            return s;
        StringBuilder[] array = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) 
            array[i] = new StringBuilder();
        boolean down = true;
        int rowIndex = 0;
        for (int i = 0; i < s.length(); i++) {
            array[rowIndex].append(s.charAt(i));
            if (down) {
                if (rowIndex < numRows - 1)
                    rowIndex++;
                else {
                    down = false;
                    rowIndex--;
                }
            } else {
                if (rowIndex > 0)
                    rowIndex--;
                else {
                    down = true;
                    rowIndex++;
                }
            }
        }
        String result = "";
        for (int i = 0; i < numRows; i++) {
            result += array[i];
        }
        return result;
    }

No comments:

Post a Comment