My java solution


  • 0
    S
     public static String convertSandy(String s, int rows){
         int length = s.length();
         StringBuilder sb = new StringBuilder(); 
         if (rows <= 1){
             return s;
         }
         int count = rows*2 - 2;
         //first row
         for(int i = 0; i<length; i+=count){
             sb.append(s.charAt(i));
         }
         //middle row
         for(int i=1;i<rows-1;i++){
             for(int j=i;j<length;j+=2*i){
                 sb.append(s.charAt(j));
                 if((j+count-2*i)<length){
                     j=j+count-2*i;
                     sb.append(s.charAt(j));
                 }else{
                     break;
                 }
             }
         }
         //last row
         for(int i =rows-1;i<length;i+=count){
             sb.append(s.charAt(i));
         }
         return sb.toString();
     }

Log in to reply
 

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