54. 螺旋矩阵
给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
List<Integer> res = new ArrayList<Integer>();
int left = 0;
int right = n - 1;
int top = 0;
int bottom = m - 1;
while(left <= right && top <= bottom){
for(int i = left; i <= right; i++){
res.add(matrix[top][i]);
}
for(int j = top + 1; j <= bottom; j++){
res.add(matrix[j][right]);
}
if(left < right && top < bottom){
for(int k = right - 1; k >= left; k--){
res.add(matrix[bottom][k]);
}
for(int g = bottom - 1; g >= top + 1; g--){
res.add(matrix[g][left]);
}
}
top++;
bottom--;
left++;
right--;
}
return res;
}
}