# 螺旋矩阵

给你一个 mn 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。

 

示例 1:

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:
[1,2,3,6,9,8,7,4,5]

示例 2:

输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:
[1,2,3,4,8,12,11,10,9,5,6,7]

 

提示:

## template ```java class Solution { public List spiralOrder(int[][] matrix) { List res = new ArrayList(); if (matrix.length == 0 || (matrix.length == 1 && matrix[0].length == 0)) return res; int left = 0; int right = matrix[0].length - 1; int top = 0; int bottom = matrix.length - 1; int num = (right + 1) * (bottom + 1); while (num > 0) { for (int j = left; j <= right; j++) { res.add(matrix[top][j]); num--; } if (num <= 0) break; top++; for (int i = top; i <= bottom; i++) { res.add(matrix[i][right]); num--; } if (num <= 0) break; right--; for (int j = right; j >= left; j--) { res.add(matrix[bottom][j]); num--; } if (num <= 0) break; bottom--; for (int i = bottom; i >= top; i--) { res.add(matrix[i][left]); num--; } if (num <= 0) break; left++; } return res; } } ``` ## 答案 ```java ``` ## 选项 ### A ```java ``` ### B ```java ``` ### C ```java ```