- Notifications
You must be signed in to change notification settings - Fork381
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
int count = 0; | ||
int row = 0; | ||
int col = 0; | ||
boolean right = true; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 (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;}
}`
No description provided.