Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug issues in Spiral Matrix Code | L4 2D Arrays #19

Open
TharunBalaji2004 opened this issue Jan 29, 2023 · 0 comments
Open

Bug issues in Spiral Matrix Code | L4 2D Arrays #19

TharunBalaji2004 opened this issue Jan 29, 2023 · 0 comments

Comments

@TharunBalaji2004
Copy link

TharunBalaji2004 commented Jan 29, 2023

I used the Spiral Matrix code for solving Leetcode problem, it seems like some of the testcases are not getting passed. I changed the code little bit, by replacing the increment/decrement variables and conditional statement for printing Bottom and Right Boundaries.
Checkout this:

public List<Integer> spiralOrder(int[][] matrix) {
    int startRow = 0;
    int startCol = 0;
    int endRow = matrix.length-1;
    int endCol = matrix[0].length-1;
    List<Integer> list = new ArrayList<Integer>();

    while(startCol <= endRow && startRow <= endCol){
        for(int i=startRow; i<=endCol; i++)
           list.add(matrix[startCol][i]);
        startCol++;

        for(int i=startCol; i<=endRow; i++)
            list.add(matrix[i][endCol]);
        endCol--;

        if (startCol <= endRow){
            for(int i=endCol; i>=startRow; i--)
                list.add(matrix[endRow][i]);
            endRow--;
        }

        if (startRow <= endCol){
            for(int i=endRow; i>=startCol; i--)
                list.add(matrix[i][startRow]);
            startRow++;
        }
    }

    return list;
}
@TharunBalaji2004 TharunBalaji2004 changed the title Bug issues in Spiral Matrix Code | L4 2D Arays Bug issues in Spiral Matrix Code | L4 2D Arrays Jan 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant