class Solution {
public int[] spiralOrder(int[][] matrix) {
if(matrix.length == 0) return new int[0];
int m = matrix.length, n = matrix[0].length;
List<Integer> res = new ArrayList<>();
int l = 0, r = n - 1, t = 0, b = m - 1;
while(res.size() < m * n){
for(int i = l; i <= r; i++) res.add(matrix[t][i]);
if(++t > b) break;
for(int i = t; i <= b; i++) res.add(matrix[i][r]);
if(--r < l) break;
for(int i = r; i >= l; i--) res.add(matrix[b][i]);
if(--b < t) break;
for(int i = b; i >= t; i--) res.add(matrix[i][l]);
if(++l > r) break;
}
return res.stream().mapToInt(x -> x).toArray();
}
}
09-03
171
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-27
194
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-10
258
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)