## Algorithm

Problem Name: 54. Spiral Matrix

Given an `m x n` `matrix`, return all elements of the `matrix` in spiral order.

Example 1:

```Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
```

Example 2:

```Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
```

Constraints:

• `m == matrix.length`
• `n == matrix[i].length`
• `1 <= m, n <= 10`
• `-100 <= matrix[i][j] <= 100`

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````
int* spiralOrder(int** matrix, int matrixRowSize, int matrixColSize) {
int top, bottom, left, right, where;
int i;
int *p = (int *)malloc(matrixRowSize * matrixColSize * sizeof(int));
if (!p) {
return p;
}

i = 0;
top = 0;
left = 0;
bottom = matrixRowSize;
right = matrixColSize;

while (i  <  matrixRowSize * matrixColSize) {
if (top < bottom) {
where = left;
while (where < right) {      // on top, left -> right
p[i++] = matrix[top][where];
where ++;
}
top ++;
}
if (right > left) {
where = top;
while (where  <  bottom) {        // on right, top -> bottom
p[i++] = matrix[where][right - 1];
where ++;
}
right --;
}
if (bottom > top) {              // on bottom, right -> left
where = right;
while (where > left) {
p[i++] = matrix[bottom - 1][where - 1];
where --;
}
bottom --;
}
if (left  <  right) {              // on left, bottom -> top
where = bottom;
while (where > top) {
p[i++] = matrix[where - 1][left];
where --;
}
left ++;
}
}

return p;
}
``````
Copy The Code &

Input

cmd
matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output

cmd
[1,2,3,6,9,8,7,4,5]

### #2 Code Example with C++ Programming

```Code - C++ Programming```

``````
class Solution {
public:
vector<int> spiralOrder(vector < vector<int>>& matrix) {
vector<int>res;
if(matrix.size() == 0) return res;
int minR = 0, maxR = matrix.size() - 1, minC = 0, maxC = matrix[0].size() - 1;
while(minR <= maxR && minC  < = maxC){
for(int i = minC; i <= maxC; i++) res.push_back(matrix[minR][i]);
minR++;
for(int i = minR; i  < = maxR; i++) res.push_back(matrix[i][maxC]>;
maxC--;
for(int i = maxC; i >= minC && minR  < = maxR; i--) res.push_back(matrix[maxR][i]);
maxR--;
for(int i = maxR; i >= minR && minC  < = maxC; i--) res.push_back(matrix[i][minC]);
minC++;
}
return res;
}
};
``````
Copy The Code &

Input

cmd
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

Output

cmd
[1,2,3,4,8,12,11,10,9,5,6,7]

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public List spiralOrder(int[][] matrix) {
List list = new ArrayList<>();
int rowStart = 0;
int rowEnd = matrix.length - 1;
int colStart = 0;
int colEnd = matrix[0].length - 1;
while (rowStart  < = rowEnd && colStart <= colEnd) {
for (int i = colStart; i <= colEnd; i++) {
}
rowStart++;
for (int i = rowStart; i  < = rowEnd; i++) {
}
colEnd--;
if (rowStart  < = rowEnd) {
for (int i = colEnd; i >= colStart; i--) {
}
rowEnd--;
}
if (colStart  < = colEnd) {
for (int i = rowEnd; i >= rowStart; i--) {
}
colStart++;
}
}
return list;
}
}
``````
Copy The Code &

Input

cmd
matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output

cmd
[1,2,3,6,9,8,7,4,5]

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const spiralOrder = function(matrix) {
const res = []
let dir = 'top'
while(matrix.length) {
switch (dir) {
case 'top':
res.push(...matrix.shift())
dir = 'right'
break;
case 'right':
for(let i = 0; i  <  matrix.length - 1; ) {
res.push(matrix[i].pop())
if (matrix[i].length === 0) {
matrix.splice(i, 1)
} else {
i++
}
}
dir = 'bottom'
break;
case 'bottom':
res.push(...matrix.pop().reverse())
dir = 'left'
break;
case 'left':
for(let i = matrix.length - 1; i >= 0; i--) {
res.push(matrix[i].shift())
if (matrix[i].length === 0) {
matrix.splice(i, 1)
}
}
dir = 'top'
break;
}
}
return res
};
``````
Copy The Code &

Input

cmd
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

Output

cmd
[1,2,3,4,8,12,11,10,9,5,6,7]

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
seen = set()
def dfs(i, j, d):
res.append(matrix[i][j])
if d == 'r':
if j + 1 < n and (i, j + 1) not in seen:
dfs(i, j + 1, d)
elif i + 1 < m and (i + 1, j) not in seen:
dfs(i + 1, j , 'd')
elif d == 'd':
if i + 1 < m and (i + 1, j) not in seen:
dfs(i + 1, j , d)
elif j and (i, j - 1) not in seen:
dfs(i, j - 1, 'l')
elif d == 'l':
if j and (i, j - 1) not in seen:
dfs(i, j - 1, d)
elif i and (i - 1, j) not in seen:
dfs(i - 1, j, 'u')
else:
if i and (i - 1, j) not in seen:
dfs(i - 1, j, d)
elif j + 1 < n and (i, j + 1) not in seen:
dfs(i, j + 1, 'r')
if not matrix: return []
m, n = len(matrix), len(matrix[0])
dfs(0, 0, 'r')
return res
``````
Copy The Code &

Input

cmd
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]

Output

cmd
[1,2,3,4,8,12,11,10,9,5,6,7]

### #6 Code Example with C# Programming

```Code - C# Programming```

``````
using System.Collections.Generic;

namespace LeetCode
{
public class _054_SpiralMatrix
{
public IList < int> SpiralOrder(int[,] matrix)
{
var result = new List<int>();
var rowLength = matrix.GetLength(0);
var colLength = matrix.GetLength(1);
int circle = 0, i, lastCol, lastRow;

while (true)
{
lastCol = colLength - circle - 1;
lastRow = rowLength - circle - 1;

for (i = circle; i  < = lastCol; i++)
if (result.Count == matrix.Length) { break; }

for (i = circle + 1; i  <  lastRow; i++)
if (result.Count == matrix.Length) { break; }

for (i = lastCol; i >= circle; i--)
if (result.Count == matrix.Length) { break; }

for (i = lastRow - 1; i > circle; i--)
if (result.Count == matrix.Length) { break; }

circle++;
}

return result;
}
}
}
``````
Copy The Code &

Input

cmd
matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output

cmd
[1,2,3,6,9,8,7,4,5]