题目
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
示例 1:
输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
示例 2:
输入:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]
代码实现
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> result = new ArrayList<>();
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
return result;
int rowNum = matrix.length, colNum = matrix[0].length;
int top = 0, right = colNum - 1, bottom = rowNum - 1, left = 0;
while (result.size() < rowNum * colNum) {
// from left to right
for (int col = left; col <= right; col++) {
result.add(matrix[top][col]);
}
top++;
// from top to buttom
if (result.size() < rowNum * colNum) {
for (int row = top; row <= bottom; row++) {
result.add(matrix[row][right]);
}
right--;
}
// from right to left
if (result.size() < rowNum * colNum) {
for (int row = right; row >= left; row--) {
result.add(matrix[bottom][row]);
}
bottom--;
}
// from buttom to top
if (result.size() < rowNum * colNum) {
for (int col = bottom; col >= top; col--) {
result.add(matrix[col][left]);
}
left++;
}
}
return result;
}
}
本文介绍了一种矩阵螺旋遍历的算法实现,通过定义四个边界,每次遍历一层后更新边界,直到遍历完整个矩阵。示例展示了如何将一个二维矩阵按顺时针螺旋顺序输出所有元素。
427

被折叠的 条评论
为什么被折叠?



