思想:
一步一步进行判断,从左往右,从上到下,从右往左,从下到上。
class Solution {
public:
vector<int> spiralOrder(vector<vector<int> > &matrix) {
vector<int> res;
if(matrix.empty()) return res;
int beginX = 0, endX = matrix[0].size() - 1;
int beginY = 0, endY = matrix.size() - 1;
while(true) {
//from left to right
for(int j = beginX; j <= endX; j++) {
res.push_back(matrix[beginY][j]);
}
if(++beginY > endY) break;
//from top to bottom
for(int i = beginY; i <= endY; i++) {
res.push_back(matrix[i][endX]);
}
if(beginX > --endX) break;
//from right to left
for(int j = endX; j >= beginX; j--) {
res.push_back(matrix[endY][j]);
}
if(beginY > --endY) break;
//from bottom to top
for(int i = endY; i >= beginY; i--) {
res.push_back(matrix[i][beginX]);
}
if(++beginX > endX) break;
}
return res;
}
};