Easy and clear JAVA solution


  • 0
    Y
    class Solution {
        public String convert(String s, int numRows) {
            if(numRows<=1)return s;
            StringBuilder[] sb = new StringBuilder[numRows];
            for(int i=0;i<sb.length;i++){
            sb[i]=new StringBuilder(""); 
        }
            int lag = numRows*2-2;
            for(int i = 0; i < s.length(); i++){
                int mod = i % lag;
                if(mod < numRows) sb[mod].append(s.charAt(i));
                else sb[lag - mod].append(s.charAt(i));
            }
            String res = "";
            for(StringBuilder sbb: sb) res += sbb;
            return res.toString();
        }
    }
    

Log in to reply
 

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