WRONG ANSWER??!!! Input: "A", 2 Output: "A" Expected: "A"


  • 0
    T

    My second solution tries to emulate the whole "writing it out in zigzag, then read horizontally" process.
    However, this weird error pops up for a simple unit test.

    58 / 1158 test cases passed.
    Input: "A", 2
    Output: "A"
    Expected: "A"

    Any help is appreciated.
    Here's source code:

    if (numRows == 1)
            return s;
        int atLeast = s.length()/(numRows+(numRows-2))+1;
        int numCols = (Math.floorMod(s.length(), numRows+(numRows-2)) < numRows) ? 2*atLeast-1 : 2*atLeast;
        char[][] mash = new char[numRows][numCols];
        int row = 0;
        int col = 0;
        boolean down = true;
        for (int i=0; i<s.length(); i++)
        {
            mash[row][col] = s.charAt(i);
            if (down)
            {
                row++;
                if (row == numRows)
                {
                    down = false;
                    row--;
                    col++;
                }
            }
            else
            {
                row--;
                if (row == 0)
                {
                    down = true;
                    col++;
                }
            }
        }
        
        StringBuffer str = new StringBuffer(s.length());
        for (int i=0; i<numRows; i++)
        for (int j=0; j<numCols; j++)
        {
            str.append(mash[i][j]);
            if (i==0 || i==numRows-1)
                j++;
        }
        
        return new String(str);

  • 0

    The FAQ covers this (fourth item). And add System.out.println(str.length()); before your return to see that you indeed have something after the "A".


  • 0
    T

    Thank you! The FAQ is very helpful indeed.


Log in to reply
 

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