# Spiral Matrix Problem Solution

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

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

## Problem Solution In Python

``````def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
res = []
r1, r2 = 0, len(matrix) - 1
c1, c2 = 0, len(matrix[0]) - 1

while r1 <= r2 and c1 <= c2:
for c in range(c1, c2 + 1):
res.append(matrix[r1][c])
for r in range(r1 + 1, r2 + 1):
res.append(matrix[r][c2])
if r1 < r2 and c1 < c2:
for c in range(c2 - 1, c1, -1):
res.append(matrix[r2][c])
for r in range(r2, r1, -1):
res.append(matrix[r][c1])

r1 += 1; r2 -= 1
c1 += 1; c2 -= 1

return res
``````

## Problem Solution In Java

``````public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<Integer>();
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return list;
int row = matrix.length;
int col = matrix[0].length;
int left = 0, right = col-1;
int top = 0, bottom = row-1;
while(true){
for(int i = left; i<=right; i++){
}
top++;
if(top > bottom) break;
for(int i = top; i<=bottom; i++){
}
right--;
if(right < left) break;
for(int i = right; i>=left; i--){
}
bottom--;
if(bottom < top) break;
for(int i = bottom; i>=top; i--){
}
left++;
if(left > right) break;
}
return list;
}
}
``````

## Problem Solution In C++

``````class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int i = 0, m = matrix.size(), n = m == 0 ? 0 : matrix[0].size(), num = 0, mi = min(m,n);
while( num < m*n && mi-2*i >= 0) {
answer.insert(answer.end(), matrix[i].begin() + i, matrix[i].end() - i );
for( int k = i + 1; k < m-i ; k++ ) answer.push_back(matrix[k][n-i-1]);
if( m - i - 1 > i ) answer.insert(answer.end(), matrix[m-i-1].rbegin() + i + 1, matrix[m-i-1].rend() - i );
for( int k = m-i-2; k >= i+1 && n-i-1>i; k-- ) answer.push_back(matrix[k][i]);
num+= (2*(m-2*i) + 2*(n-2*i) - 4); i++;
}
}
};
``````

## Problem Solution In C

``````int* spiralOrder(int** m, int matrixRowSize, int* matrixColSize, int* ret_size){
if(matrixRowSize==0){
*ret_size=0;
return NULL;
}

int col=*matrixColSize;
int rows=matrixRowSize;

int start_row=0;   int end_row=rows-1;
int start_col=0;   int end_col=col-1;

*ret_size=rows*col;

int* ret_arr=(int*)malloc((*ret_size)*sizeof(int));
int ret_i=0;

while(ret_i<(rows*col)){
for(int i=start_col;i<=end_col;i++){
ret_arr[ret_i++]=m[start_row][i];
}
start_row++;
if(ret_i==(rows*col)) break; //These stmts are required when R!=C (rectangular matrices)
for(int i=start_row;i<=end_row;i++){
ret_arr[ret_i++]=m[i][end_col];
}
end_col--;
if(ret_i==(rows*col)) break;
for(int i=end_col;i>=start_col;i--){
ret_arr[ret_i++]=m[end_row][i];
}
end_row--;
if(ret_i==(rows*col)) break;
for(int i=end_row;i>=start_row;i--){
ret_arr[ret_i++]=m[i][start_col];
}
start_col++;
if(ret_i==(rows*col)) break;
}
return ret_arr;

}``````

If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.