时间: 2020-11-23|27次围观|0 条评论

题目描述:(链接

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

解题思路:

先将矩阵选择180度,然后再将矩阵上下折叠。

 1 class Solution {
 2 public:
 3     void rotate(vector<vector<int> > &matrix) {
 4         int temp;
 5         int len = matrix.size();
 6         
 7         // 现将矩阵旋转180°
 8         for(int i = 0; i < len ; ++i){
 9             for(int j = 0; j < len; ++j){
10                 if(i + j <= len - 1){
11                     temp = matrix[i][j];
12                     matrix[i][j] = matrix[len - j - 1][len - i - 1];
13                     matrix[len - j - 1][len - i - 1] = temp;
14                 }
15             }
16         }
17         
18         // 将矩阵上下反转
19         for(int i = 0; i < len / 2; ++i){
20             for(int j = 0; j < len; ++j){
21                 temp = matrix[i][j];
22                 matrix[i][j] = matrix[len - 1 - i][j];
23                 matrix[len - 1 - i][j] = temp;
24             }
25         }
26         
27     }
28 };

 

转载于:https://www.cnblogs.com/skycore/p/4870235.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/99077058

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《[LeetCode]Rotate Image
   

还没有人抢沙发呢~