剑指offer【面试题20:顺时针打印矩阵】

题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下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.
 
 
思路:
1、对任意一个矩阵,获取其外围边界,按题目要求顺序放进数组(顺序顶部、右边、底部、左边);
2、缩小给定矩阵(剥离外围边界)
重复步骤1、2即可。
注意:注意当矩阵为行或者列向量的时候,遍历可能会存在越界问题,你可以作特殊处理
  1 //输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,
  2 //例如,如果输入如下4 X 4矩阵: 
  3 /*
  4 1  2  3  4
  5 5  6  7  8
  6 9  10 11 12
  7 13 14 15 16
  8 */
  9 //则依次打印出数字1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10.
 10 #include<iostream>
 11 #include<vector>
 12 #include<algorithm>
 13 using namespace std;
 14 // 打印vector<int>
 15 void myprintfVec(vector<int>& vec)
 16 {
 17     if (vec.empty())
 18     {
 19         cout << "empty !" << endl;
 20         return;
 21     }
 22     else
 23     {
 24         for (auto& ele:vec)
 25         {
 26             cout << ele << " ";
 27         }
 28         cout << endl;
 29     }
 30 }
 31 // 打印vector<vector<int>>
 32 void myprintfVecs(vector<vector<int>> vecs)
 33 {
 34     if (vecs.empty())
 35     {
 36         cout << "vector<vector<int>> empty!" << endl;
 37     }
 38     else
 39     {
 40         int rows = vecs.size();
 41         int cols = vecs[0].size();
 42         for (int j = 0; j < rows; j++)
 43         {
 44             for (int i = 0; i < cols; i++)
 45             {
 46                 cout.width(3);
 47                 cout << vecs[j][i] << " ";
 48             }
 49             cout << endl;
 50         }
 51     }
 52     
 53 }
 54 // 获取一个矩阵的边界
 55 vector<int> getTheBorder(const vector<vector<int>> matrix)
 56 {
 57     vector<int> top, bottom, left, right;
 58     const int rows = matrix.size();
 59     const int cols = matrix[0].size();
 60     vector<int> result;
 61     //顺时针
 62     if ((rows == 1) || (cols == 1)) // 当给定矩阵是行、列向量的时候
 63     {
 64         for (int j = 0; j < rows; j++)
 65         {
 66             for (int i = 0; i < cols; i++)
 67             {
 68                 result.push_back(matrix[j][i]);
 69             }
 70         }
 71     }
 72     else
 73     {
 74         // 顶部
 75         for (int i = 0; i < cols; i++)
 76         {
 77             result.push_back(matrix[0][i]);
 78         }
 79         //myprintfVec(top);
 80         //右列
 81         for (int j = 1; j < (rows - 1); j++)
 82         {
 83             result.push_back(matrix[j][cols - 1]);
 84         }
 85         //myprintfVec(right);
 86         //底部
 87         for (int m = cols - 1; m >= 0; m--)
 88         {
 89             result.push_back(matrix[rows - 1][m]);
 90         }
 91         //myprintfVec(bottom);
 92         //左列
 93         for (int n = (rows - 2); n > 0; n--)
 94         {
 95             result.push_back(matrix[n][0]);
 96         }
 97         //myprintfVec(left);
 98     }
 99     //myprintfVec(result);
100     return result;
101 }
102 // 
103 void printMatrix(vector<vector<int> >& matrix) 
104 {
105     const int rows = matrix.size();
106     const int cols = matrix.at(0).size();
107     // 比如 4x5的矩阵,你要剥离 2圈; 7x8的矩阵,你要剥离 4圈
108     int totalsteps = round(min(rows, cols)/2.0); //剥离圈数
109     vector<int> array_;
110     for (int step = 0; step < totalsteps; step++)// 一共剥离 totalsteps 圈
111     {
112         vector<vector<int>> matrix_;
113         vector<int> temp;
114         // 构建的矩阵matrix_ 初始化
115         for (int i = 0; i < cols - 2*step; i++)
116             temp.push_back(0);
117         for (int j = 0; j < rows - 2*step; j++)
118             matrix_.push_back(temp);
119         //myprintfVecs(matrix_);
120         // 剥离后的矩阵matrix_
121         for ( int j = 0; j < rows - 2 * step; j++)
122         {
123             for (int i = 0; i < cols - 2 * step; i++)
124             {
125                 matrix_[j][i] = matrix[j+step][i+step];
126             }
127         }
128         // 获取边界
129         vector<int> border = getTheBorder(matrix_);
130         myprintfVec(border);
131     }
132 }
133 
134 void dosomeboring(vector<int>& vec,const int add)
135 {
136     for (size_t i = 0; i < vec.size(); i++)
137     {
138         vec[i] += add;
139     }
140 }
141 
142 int main()
143 {
144     const int rows = 15;
145     const int cols = 5;
146     vector<vector<int>> vecs;
147     // 创建一个矩阵
148     for (size_t i = 0; i < rows; i++)
149     {
150         vector<int> vec;
151         for (size_t i = 0; i < cols; i++)
152             vec.push_back(i);
153 
154         dosomeboring(vec, i);
155         vecs.push_back(vec);
156     }
157     // 打印上述创建的矩阵
158     myprintfVecs(vecs);
159 
160     // 按照题目傻逼要求打印
161     printMatrix(vecs);
162     return 1;
163 }

原文地址:https://www.cnblogs.com/winslam/p/9457577.html