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

剑指offer--26.顺时针打印矩阵

时间:2019-03-29 19:15:19      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:color   public   for   tar   空间   center   com   输入   tag   

1,2,3,4
5,6,7,8
8,10,11,12
13,14,15,16

每次输出第一行,然后删除第一行,逆时针旋转剩下的矩阵。

------------------------------------------------------------------------------------------------------------------------

时间限制:1秒 空间限制:32768K 热度指数:430225
本题知识点: 数组

题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
 
class Solution {
    public:
        vector<int> printMatrix(vector<vector<int> > matrix) {
            if(matrix.empty() || matrix.size() == 1)  return matrix[0];
            vector<int> ans;
            while(!matrix.empty()) {
                for(int x:matrix[0]) {
                    ans.push_back(x);
                }
                vector<vector<int>> cp;
                for(int i=0; i<matrix[0].size(); i++) {
                    vector<int> tmp;
                    for(int j=1; j<matrix.size(); j++) {
                        tmp.push_back(matrix[j][i]);
                    }
                    cp.push_back(tmp);
                }
                reverse(cp.begin(), cp.end());
                matrix = cp;
            }
            return ans;
        }
};

 

剑指offer--26.顺时针打印矩阵

标签:color   public   for   tar   空间   center   com   输入   tag   

原文地址:https://www.cnblogs.com/evidd/p/10623190.html

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