I found this more intuitive :)


  • 0
    R

    If this is more intuitive? Hope it helps. Thanks!

    class Solution {
        public String convert(String s, int numRows) {
            if(s == null || s.length() == 0) return "";
            StringBuffer[] arr = new StringBuffer[numRows];
            for(int i = 0; i < numRows; i++) {
                arr[i] = new StringBuffer();
            }
            int flag = 1;
            int count = 0;
            for(int i = 0; i < s.length(); i++) {
                arr[count].append(s.charAt(i));
                if(count == 0) {
                    flag = 1;
                } else if(count == numRows-1) {
                    flag = -1;
                }
                if(0 != numRows-1)
                count += flag;
            }
            for(int j = 1; j < numRows; j++) {
                arr[0].append(arr[j]);
            }
            return arr[0].toString();
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.