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

(leetcode)Rotate Image

时间:2015-08-21 10:56:26      阅读:174      评论:0      收藏: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?

使用先对角线翻转,后水平翻转

1      2              对角线翻转      4           2          水平翻转         3          1 

3      4             =========>    3           1       ==========>   4          2

 1 class Solution {
 2 public:
 3     void rotate(vector<vector<int>>& matrix) {
 4         int n = matrix.size();
 5         int temp;
 6         for(int i = 0;i < n;++i)
 7         {
 8             for(int j = 0;j < n-i;++j)
 9             {
10                 temp = matrix[i][j];
11                 matrix[i][j] = matrix[n-j-1][n-i-1];
12                 matrix[n-j-1][n-i-1] = temp;
13             }
14         }
15         
16         for(int i = 0; i < n/2; ++i)//沿着水平线翻转
17         {
18             for(int j = 0;j < n;++j)
19             {
20                 temp = matrix[i][j];
21                 matrix[i][j] = matrix[n-i-1][j];
22                 matrix[n-i-1][j] = temp;
23             }
24         }
25     }
26 };

 

 

 

(leetcode)Rotate Image

标签:

原文地址:http://www.cnblogs.com/chdxiaoming/p/4747057.html

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