注意矩阵为空的情况。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) {
return {};
}
vector<int> res;
int m = matrix.size(); //行数
int n = matrix[0].size(); //列数
int up = 0;
int down = m - 1;
int left = 0;
int right = n - 1;
while (true) {
for (int i = left; i <= right; i++) {
res.push_back(matrix[up][i]);
}
if (++up > down) {
break;
}
for (int i = up; i <= down; i++) {
res.push_back(matrix[i][right]);
}
if (--right < left) {
break;
}
for (int i = right; i >= left; i--) {
res.push_back(matrix[down][i]);
}
if (--down < up) {
break;
}
for (int i = down; i >= up; i--) {
res.push_back(matrix[i][left]);
}
if (++left > right) {
break;
}
}
return res;
}
};