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

Rotate Image

时间:2014-08-27 16:06:57      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   ar   div   log   amp   

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?

思路:对于matrix[i][j],其顺时针旋转:1) 90度对应的点为matrix[j][n-1-i];  2) 180度对应的点为matrix[n-1-i][n-1-j];  3) 270度对应的点为matrix[n-1-j][i]。注意边界条件。

 1 class Solution {
 2 public:
 3     void rotate( vector<vector<int>> &matrix ) {
 4         int n = matrix.size();
 5         for( int i = 0; i < n/2; ++i ) {
 6             for( int j = i; j < n-i-1; ++j ) {
 7                 int tmp = matrix[i][j];
 8                 matrix[i][j] = matrix[n-1-j][i];
 9                 matrix[n-1-j][i] = matrix[n-1-i][n-1-j];
10                 matrix[n-1-i][n-1-j] = matrix[j][n-1-i];
11                 matrix[j][n-1-i] = tmp;
12             }
13         }
14         return;
15     }
16 };

 

Rotate Image

标签:style   blog   color   io   for   ar   div   log   amp   

原文地址:http://www.cnblogs.com/moderate-fish/p/3939359.html

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