首页 > 其他 > 详细

之字形打印矩阵

时间:2016-04-03 15:46:02      阅读:169      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!