您的位置:首页 > 其它

循环数组打印

2016-05-20 22:04 316 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: