首页 > 代码库 > Rotate Image

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?

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