码迷,mamicode.com
首页 > 其他好文 > 详细

之字形打印矩阵

时间:2016-04-03 15:46:20      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

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

给定一个整数矩阵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]

Solution 1:
class Printer {
public:
    vector<int> printMatrix(vector<vector<int> > mat, int n, int m) {
        // write code here
        vector<int> v;
        for(int i = 0; i < n; ++i) {
            vector<int> temp = mat[i];
            if(i % 2) reverse(temp.begin(), temp.end());
            for(int j = 0; j < m; ++j) {
                v.push_back(temp[j]);
            }
        }
        return v;
    }
};

之字形打印矩阵

标签:

原文地址:http://www.cnblogs.com/xuyan505/p/5349813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!