easy solution


  • 0
    L
    public class Solution {
        public String convert(String s, int numRows) {
            if(s==null|| "".equals(s) || numRows==1 || numRows>=s.length()) return s;
    
            String[] result = new String[numRows];
            for(int i=0;i<result.length;i++) {
                StringBuilder builder = new StringBuilder();
                int n = (numRows - i - 1) * 2;
                for(int j=i;j<s.length();) {
                    builder.append(s.charAt(j));
                    if (n%((numRows-1)*2)!=0 && j + n < s.length()) builder.append(s.charAt(j + n));
    
                    j = j + (numRows - 1) * 2;
                }
    
                result[i] = builder.toString();
            }
            StringBuilder builder = new StringBuilder();
            for(String it:result) builder.append(it);
    
            return builder.toString();
    }
    }
    

Log in to reply
 

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