Leetcode: Rotate Image

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

 

posted @ 2013-05-02 14:36  caijinlong  阅读(114)  评论(0编辑  收藏  举报