Solution for a misinterpretation of the zigzag pattern

  • 0

    Thinking that the zigzag pattern was obliquely down and obliquely up, I was able to come up with a quite different solution:

    public String convert(String s, int numRows) {
            if (numRows==1) return s;
            StringBuilder zz = new StringBuilder();
            for (int i=0, n=numRows-1, m=0; i<numRows; i++, n--, m++)
                int j=i;
                int prev=-1;
                boolean o = true;
                while (j<s.length())
                    if (prev!=j) zz.append(s.charAt(j));
                    j = o ? j+2*n : j+2*m;
                    o = !o;
            return zz.toString();

    Solution is pretty hard to understand and a bit impractical but it surprisingly passed all test cases.

Log in to reply

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