A intuitive solution by java


  • 0
    W

    the j is used to enumerate vertical columns and the k is used to enumerate oblique columns

    public class Solution {
        public String convert(String s, int numRows) {
            if (numRows < 2) return s;
            StringBuilder sb = new StringBuilder(s.length());
            int offset = 2*numRows-2;
            for (int i = 0; i < numRows; i++) {
                int k = offset-i;
                for (int j = i; j < s.length(); j += offset) {
                    sb.append(s.charAt(j));
                    if (i != 0 && i != numRows-1) {
                        if (k < s.length()) {
                            sb.append(s.charAt(k));
                            k += offset;
                        }
                    }
                }
            }
            return sb.toString();
        }
    }
    

Log in to reply
 

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