ZigZag Conversion


  • 0
    P
    public String convert(String s, int numRows) {
            if(s==null || s.length() == 0 || numRows ==1) return s;
            StringBuilder arr[] = new StringBuilder[numRows];
            int oper = 1,index=0;
            for(int i=0;i<s.length();i++) {
                if(arr[index] == null) arr[index] = new StringBuilder();
                arr[index].append(s.charAt(i));
                index = index + oper;
                  if(index == numRows-1) {
                    oper = -1;
                } else if(index == 0) {
                    oper = 1;
                }
            }
            for(int i=1;i<numRows;i++) {
                if(arr[i]!=null) arr[0].append(arr[i]);
            }
            return arr[0].toString();
        }
    

Log in to reply
 

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