力扣算法题—048旋转图像

  1 #include "000库函数.h"
  2 
  3 //找位置规律
  4 //先不按照规则,使用另一个矩阵
  5 class Solution {
  6 public:
  7     void rotate(vector<vector<int>>& matrix) {
  8         vector < vector<int>>v = matrix;
  9         int n = matrix.size();
 10         for (int t = 0; t < 1; ++t) {
 11             for (int i = 0; i < n; ++i) {
 12                 for (int j = 0; j < n; ++j) {
 13                     matrix[j][n - 1 - i] = v[i][j];
 14                 }
 15             }
 16         }
 17     }
 18 };
 19 
 20 //1  2  3                 7  2  1                  7  4  1
 21 //
 22 //4  5  6      -- >          4  5  6   -- >           8  5  2
 23 //
 24 //7  8  9                 9  8  3                9  6  3
 25 //
 26 
 27 class Solution {
 28 public:
 29     void rotate(vector<vector<int> > &matrix) {
 30         int n = matrix.size();
 31         for (int i = 0; i < n / 2; ++i) {
 32             for (int j = i; j < n - 1 - i; ++j) {
 33                 int tmp = matrix[i][j];
 34                 matrix[i][j] = matrix[n - 1 - j][i];
 35                 matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
 36                 matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
 37                 matrix[j][n - 1 - i] = tmp;
 38             }
 39         }
 40     }
 41 };
 42 
 43 //还有一种解法,首先以从对角线为轴翻转,然后再以x轴中线上下翻转即可得到结果,如下图所示(其中蓝色数字表示翻转轴):
 44 //
 45 //1  2  3       9  6  3       7  4  1
 46 //
 47 //4  5  6  -- >    8  5  2   -- >   8  5  2
 48 //
 49 //7  8  9       7  4  1       9  6  3
 50 
 51 class Solution {
 52 public:
 53     void rotate(vector<vector<int> > &matrix) {
 54         int n = matrix.size();
 55         for (int i = 0; i < n - 1; ++i) {
 56             for (int j = 0; j < n - i; ++j) {
 57                 swap(matrix[i][j], matrix[n - 1 - j][n - 1 - i]);
 58             }
 59         }
 60         for (int i = 0; i < n / 2; ++i) {
 61             for (int j = 0; j < n; ++j) {
 62                 swap(matrix[i][j], matrix[n - 1 - i][j]);
 63             }
 64         }
 65     }
 66 };
 67 
 68 //最后再来看一种方法,这种方法首先对原数组取其转置矩阵,然后把每行的数字翻转可得到结果,如下所示(其中蓝色数字表示翻转轴):
 69 //
 70 //1  2  3       1  4  7       7  4  1
 71 //
 72 //4  5  6  -- >     2  5  8   -- >   8  5  2
 73 //
 74 //7  8  9       3  6  9         9  6  3
 75 
 76 
 77 class Solution {
 78 public:
 79     void rotate(vector<vector<int> > &matrix) {
 80         int n = matrix.size();
 81         for (int i = 0; i < n; ++i) {
 82             for (int j = i + 1; j < n; ++j) {
 83                 swap(matrix[i][j], matrix[j][i]);
 84             }
 85             reverse(matrix[i].begin(), matrix[i].end());
 86         }
 87     }
 88 };
 89 void T048() {
 90     Solution s;
 91     vector < vector<int>>n;
 92     n = {
 93         {1, 2, 3},
 94         {4, 5, 6},
 95         {7, 8, 9}
 96     };
 97     cout << "原矩阵:" << endl;
 98     for (auto &a : n) {
 99         for (auto b : a)
100             cout << b << "  ";
101         cout << endl;
102     }
103     cout << "旋转之后:" << endl;
104     s.rotate(n);
105     for (auto &a : n) {
106         for (auto b : a)
107             cout << b << "  ";
108         cout << endl;
109     }
110     
111 }

 

posted @ 2019-03-28 15:07  自由之翼Az  阅读(251)  评论(0编辑  收藏  举报