Monday, November 25, 2013

LeetCode Problem : Rotate Image

Problem


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?

Code

void rotate(vector<vector<int> >& A)
{
    int n = A.size();
    for(int i = 0; i < n/2; ++i){
        for(int k = i; k < n - 1 - i; ++k){
            int t1 = A[k][n - 1 - i];
            int t2 = A[n - 1 - i][n - 1 - k];
            int t3 = A[n - 1 - k][i];
            A[k][n - 1 - i] = A[i][k];
            A[n - 1 - i][n - 1 - k] = t1;
            A[n - 1 - k][i] = t2;
            A[i][k] = t3;
        }
    }
}

No comments:

Post a Comment