class Printer {
public:
vector<int> clockwisePrint(vector<vector<int> > mat, int n, int m) {
// write code here
vector<int> buf;
if(mat.empty())
return buf;
int st_x=0;
int end_x=n-1;
int st_y=0;
int end_y=m-1;
int i=0;
int j=0;
while(st_x<=end_x&&st_y<=end_y){
if(st_x==end_x){
for(;j<=end_y;j++)
buf.push_back(mat[i][j]);
return buf;
}
if(st_y==end_y){
for(;i<=end_x;i++)
buf.push_back(mat[i][j]);
return buf;
}
//第一行
for(;j<end_y;j++)
buf.push_back(mat[i][j]);
//第一列
for(;i<end_x;i++)
buf.push_back(mat[i][j]);
//第二行
for(;j>st_y;j--)
buf.push_back(mat[i][j]);
//第二列
for(;i>st_x;i--)
buf.push_back(mat[i][j]);
i++;
j++;
st_x++;
st_y++;
end_x--;
end_y--;
}
return buf;
}
};
循环数组打印
最新推荐文章于 2023-12-09 23:00:00 发布