---恢复内容开始---

You are given an n x n 2D matrix representing an image.

Ratate the image by 90 degrees(clockwise).

Follow up:Could you do this in-place?

分析如下:

比较简单的一道题目。先沿着对角线(左下到右上)做对称变换,然后沿着竖直中心轴做对称变换。

 

---恢复内容结束---

 

class Solution {

public :

void rotate(vector <vector<int>>&matrix ){

      if (matrix.empty())

              return ;

int n=matrix.size();

//沿着对角线(左上到右下)做对称变换

for (int k=0;k<n;k++)

{

for (int i=0;i<k;i++)

{

int tmp=matrix[k][i];

matrix[k][i]=matrix[i][k];

matrix[i][k]=tmp;

}
}

//沿着竖直中心轴做对称变换

for (int k=0;k<n;k++){

int s=0;

int t=n-1;

while (s<t){

int tmp=matrix[k][s];

matrix[k][s]=matrix[k][s];

matrix[k][t]=tmp;

s++;

t--;

}

}

return ;

}

}

posted on 2018-01-27 15:48  HE不言  阅读(99)  评论(0编辑  收藏  举报