Rotate Image

来源:互联网 发布:手机破解压缩文件软件 编辑:程序博客网 时间:2024/05/01 05:43

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?



class Solution {public:    void rotate(vector<vector<int> > &matrix) {        int n=matrix.size();                 for(int i=0;i<n;i++)            for(int j=0;j<n-i;j++){                int temp=matrix[i][j];                matrix[i][j]=matrix[n-1-j][n-1-i];                matrix[n-1-j][n-1-i]=temp;            }        for(int i=0;i<n/2;i++)            for(int j=0;j<n;j++){                int temp=matrix[i][j];                matrix[i][j]=matrix[n-i-1][j];                matrix[n-i-1][j]=temp;            }    }};





0 0