Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/80b31247d105b129380dc9fdf5896a59 to your computer and use it in GitHub Desktop.
Save jianminchen/80b31247d105b129380dc9fdf5896a59 to your computer and use it in GitHub Desktop.
Spiral Matrix - Leetcode 54 - 2017 May 23 practice - the code passes leetcode online judge
public class Solution {
public IList<int> SpiralOrder(int[,] matrix) {
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int total = rows * cols;
var spiral = new int[total];
int index = 0;
int leftCol = 0;
int rightCol = cols - 1;
int topRow = 0;
int bottomRow = rows - 1;
// the idea is to simplify the while loop - one variable checking, check the length of spiral
// array intead of compared to the start position of spiral layer.
// test cases: one node in the array, one row, one column
while (index < total)
{
// top row - go right,
for (int col = leftCol; col <= rightCol; col++)
{
var current = matrix[topRow, col];
spiral[index++] = current;
}
// right col - go down
for (int row = topRow + 1; row <= bottomRow; row++)
{
var current = matrix[row, rightCol];
spiral[index++] = current;
}
// go left
for (int col = rightCol - 1; col >= leftCol && bottomRow > topRow; col--)
{
var current = matrix[bottomRow, col];
spiral[index++] = current;
}
// go up
for (int row = bottomRow - 1; row > topRow && leftCol < rightCol; row--)
{
var current = matrix[row, leftCol];
spiral[index++] = current;
}
leftCol++;
rightCol--;
topRow++;
bottomRow--;
}
return new List<int>(spiral);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment