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? 
Hide Tags
矩阵旋转的方法:
先左右互换,然后
顺时针则左上右下对角互换,
逆时针则左下右上对角互换。
class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        if(n)
        {
            for(int ii=0; ii<n; ++ii)
                for(int jj=0; jj<(n/2); ++jj)
                    swap(matrix[ii][jj], matrix[ii][n-1-jj]);
            for(int ii=0; ii<n; ++ii)
                for(int jj=0; jj<(n-ii); ++jj)
                    swap(matrix[ii][jj], matrix[n-1-jj][n-1-ii]);
        }
    }
};
4 ms.
 
 
No comments:
Post a Comment