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 void rotate(vector<vector<int> > &matrix)
02 {
03    int n=matrix.size();
04    if(n==0)
05          return;
06    int start=0;
07    int end=n1;
08    while(start<end){
09        for(int i=0;i<endstart;i++){
10              int tmp=matrix[start][start+i];
11              matrix[start][start+i]=matrix[endi][start];
12              matrix[endi][start]=matrix[end][endi];
13              matrix[end][endi]=matrix[start+i][end];
14              matrix[start+i][end]=tmp;
15         }
16         start++;
17         end;
18    }
19 }
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