分类:algorithm| 发布时间:2016-12-21 22:07:00
给定一个包含 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:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.empty()) return {};
int m = matrix.size(), n = matrix[0].size();
vector<int> spiral(m * n);
int u = 0, d = m - 1, l = 0, r = n - 1, k = 0;
while (true) {
// up
for (int col = l; col <= r; col++) spiral[k++] = matrix[u][col];
if (++u > d) break;
// right
for (int row = u; row <= d; row++) spiral[k++] = matrix[row][r];
if (--r < l) break;
// down
for (int col = r; col >= l; col--) spiral[k++] = matrix[d][col];
if (--d < u) break;
// left
for (int row = d; row >= u; row--) spiral[k++] = matrix[row][l];
if (++l > r) break;
}
return spiral;
}
};