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?

C++:
01 class Solution {
02 public:
03     void rotate(vector<vector<int> > &matrix) {
04         // Start typing your C/C++ solution below
05         // DO NOT write int main() function
06         int n=matrix.size();
07         int t=n/2;
08         for(int i=0;i<t;i++)
09         {
10             for(int j=0;j<n2*i1;j++)
11             {
12                 int tmp=matrix[i][i+j];
13                 matrix[i][i+j]=matrix[n1ij][i];
14                 matrix[n1ij][i]=matrix[n1i][n1ij];
15                 matrix[n1i][n1ij]=matrix[i+j][n1i];
16                 matrix[i+j][n1i]=tmp;
17             }
18         }
19     }
20 };
Advertisements
This entry was posted in Array and linked list. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s