Problems with [[1]]


  • 0
    P

    Here's my solution:

    int *spiralOrder(int **matrix, int numRows, int numColumns) {
    int v[numRows*numColumns];
    int xi=0, yi=0, circ=0;
    for (int i=0; i<numRows*numColumns; ++i)
    {
        v[i] = matrix[yi][xi];
        if (yi==circ)
        {
            if (xi<numRows-circ-1)
                ++xi;
            else
                ++yi;
        }
        else
        if (xi==numRows-circ-1)
        {
            if (yi<numColumns-circ-1)
                ++yi;
            else
                --xi;
        }
        else
        if (yi==numColumns-circ-1)
        {
            if (xi>circ)
                --xi;
            else
                --yi;
        }
        else
        {
            if (yi>circ+1)
                --yi;
            else
            {
                ++xi;
                ++circ;
            }
        }
    }
    return v;
    }
    

    Does anybody know why it gives runtime error on [[1]] input?


Log in to reply
 

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