要求:rt
思路:注意剩下一行或者一列
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix.size()==0)return {};
int m=matrix.size(),n=matrix[0].size();
vector<int> v(m*n);
int index=0;
int up=0,down=m-1,left=0,right=n-1;
while(up<=down&&left<=right){
if(up==down){
for(int i=left;i<=right;++i)
v[index++]=matrix[up][i];
break;
}
if(left==right){
for(int i=up;i<=down;++i)
v[index++]=matrix[i][right];
break;
}
for(int i=left;i<right;++i)
v[index++]=matrix[up][i];
for(int i=up;i<down;++i)
v[index++]=matrix[i][right];
for(int i=right;i>left;--i)
v[index++]=matrix[down][i];
for(int i=down;i>up;--i)
v[index++]=matrix[i][left];
++left;
++up;
--down;
--right;
}
return v;
}
};