8000 Created Spiral_Matrix code from leetcode by 02-Shivaraj · Pull Request #759 · codemistic/Data-Structures-and-Algorithms · GitHub
[go: up one dir, main page]

Skip to content

Created Spiral_Matrix code from leetcode #759

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

02-Shivaraj
Copy link

No description provided.

int count = 0;
int row = 0;
int col = 0;
boolean right = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many boolean flags for directions

  • right, left, up, down, and curr lead to bloated logic and repetitive conditionals.
  • This could be replaced by a direction array and an index to keep it clean.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`public List spiralOrder(int[][] matrix) {
List result = new ArrayList<>();
if (matrix == null || matrix.length == 0) return result;

    int top = 0;
    int bottom = matrix.length - 1;
    int left = 0;
    int right = matrix[0].length - 1;

    while (top <= bottom && left <= right) {
        // move right
        for (int i = left; i <= right; i++) {
            result.add(matrix[top][i]);
        }
        top++;

        // move down
        for (int i = top; i <= bottom; i++) {
            result.add(matrix[i][right]);
        }
        right--;

        // move left
        if
7A1A
 (top <= bottom) {
            for (int i = right; i >= left; i--) {
                result.add(matrix[bottom][i]);
            }
            bottom--;
        }

        // move up
        if (left <= right) {
            for (int i = bottom; i >= top; i--) {
                result.add(matrix[i][left]);
            }
            left++;
        }
    }

    return result;
}

}`

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

Successfully merging this pull request may close these issues.

2 participants
0