首页 > 其他 > 详细

【Leetcode】Rotate Image

时间:2014-03-20 01:50:31      阅读:420      评论: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?

bubuko.com,布布扣
 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 - 1 - i; ++j){  
 7                 int t = 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] = t;  
12             }  
13         }
14     }
15 };
View Code

由边缘至内部,一圈一圈地旋转。

另外的方法:先将图像沿副对角线翻转,再上下翻转。

【Leetcode】Rotate Image,布布扣,bubuko.com

【Leetcode】Rotate Image

原文:http://www.cnblogs.com/dengeven/p/3612464.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!