其他 之字形打印矩阵 STL
题目描述
对于一个矩阵,请设计一个算法,将元素按“之”字形打印。具体见样例。
给定一个整数矩阵mat,以及他的维数nxm,请返回一个数组,其中元素依次为打印的数字。
测试样例:
[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],4,3
返回:[1,2,3,6,5,4,7,8,9,12,11,10]
class Printer {
public:
vector<int> printMatrix(vector<vector<int> > mat, int n, int m) {
// write code here
int flag=1;
vector<int> a;
for(int i=0;i<n;++i)
{
if(flag)
{
for(int j=0;j<m;++j)
{
a.push_back(mat[i][j]);
}
flag=0;
}else
{
for(int j=m-1;j>=0;--j)
{
a.push_back(mat[i][j]);
}
flag=1;
}
}
return a;
}
};