Friday, May 5, 2017

LeetCode 2nd time 6. ZigZag Conversion Leetcode Java

     public String convert(String s, int numRows) {  
     if(s==null || numRows<=1 ||numRows>=s.length()) return s;  
     int l=s.length();  
     StringBuilder sb=new StringBuilder();  
     for(int i=0;i<numRows;i++){  
       int j=i;  
       while(j<l){  
         sb.append(s.charAt(j));  
         if((i!=0) && (i!=numRows-1) && ((j+(numRows-1-i)*2)<l)) sb.append(s.charAt(j+(numRows-1-i)*2));  
         j+=(numRows-1)*2;  
       }  
     }  
     return sb.toString();  
   }  

No comments:

Post a Comment