之字形打印矩阵

时间限制:3秒 空间限制:32768K 热度指数:14198
本题知识点: 编程基础 数组

题目描述

对于一个矩阵,请设计一个算法,将元素按“之”字形打印。具体见样例。

给定一个整数矩阵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]
 1 class Printer {
 2 public:
 3     vector<int> printMatrix(vector<vector<int> > mat, int n, int m) {
 4         // write code here
 5         vector<int>res;
 6         int row=0;
 7         
 8         for(int i=0;i<n;i++)
 9             if(i%2!=0)
10                 reverse(mat[i].begin(),mat[i].end());
11          for(int i=0;i<n;i++)
12             for(int j=0;j<m;j++)
13                 res.push_back(mat[i][j]);
14           
15        return res;
16         
17     }
18 };

 

posted @ 2017-06-05 20:03  懒小小喵  阅读(230)  评论(0)    收藏  举报