Simple Java Solution


  • 0
    public class Solution {
        public String convert(String s, int numRows) {
            if(numRows <= 1) return s;
            StringBuffer[] sb = new StringBuffer[numRows];
            for(int i=0; i<numRows; i++) sb[i] = new StringBuffer();
            int t = numRows * 2 - 2;
            for(int i=0; i<s.length(); i++){
                int j = i % t;
                if(j < numRows) sb[j].append(s.charAt(i));
                else sb[t-j].append(s.charAt(i));
            }
            for(int i=1; i<numRows; i++) sb[0].append(sb[i]);
            return sb[0].toString();
        }
    }

Log in to reply
 

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