The 8ms Java Solution in O(n) time use iteration


  • 0
    A
    public class Solution {
        public String convert(String text, int nRow) {
            if(nRow<2)
                return text;
            int g = (2*nRow - 2);
            StringBuilder sb = new StringBuilder(text.length());
            for(int k = 0; k < nRow;k++){
                for(int i=k; i < text.length(); ){
                    sb.append(text.charAt(i));
                    if(k==0||k==nRow-1)
                        i+=g;
                    else if(i%g>nRow-1)
                        i+=2*k;
                    else
                        i+=(g-2*k);
                }
            }
            return sb.toString();
        }
    }

Log in to reply
 

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