The nineteenth question of the sword finger offer
The topic is as follows
Idea and code
Just simulate it directly, there is nothing left to say, the square is done!
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
int row=matrix.size();
int col=matrix[0].size();
vector<int> res;
if(row==0||col==0) return res;
int left=0,top=0,right=col-1,bottom=row-1;
while(left<=right&&top<=bottom){
for(int i=left;i<=right;i++) res.push_back(matrix[top][i]);
for(int i=top+1;i<=bottom;i++) res.push_back(matrix[i][right]);
for(int i=right-1;i>=left;i--) res.push_back(matrix[bottom][i]);
for(int i=bottom-1;i>top;i--) res.push_back(matrix[i][left]);
left++,top++,right--,bottom--;
}
return res;
}
};