Task:
思路:
设置四个边界,然后走一圈
代码:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int>re;
if(matrix.empty() || matrix[0].empty()) return re;
int l=0, r=matrix[0].size()-1, up=0, low=matrix.size()-1;
while(l<=r && up<=low){
for(int j=l; j<=r; ++j)
re.push_back(matrix[up][j]);
for(int i=up+1; i<=low; ++i)
re.push_back(matrix[i][r]);
for(int j=r-1; low>up && j>=l; --j)
re.push_back(matrix[low][j]);
for(int i=low-1; r>l && i>up; --i)
re.push_back(matrix[i][l]);
++l, --r, ++up, --low;
}
return re;
}
};