48. Rotate Image (Array)

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 temp;
        for(int i = 0; i < matrix.size()/2; i++)
        {
            for(int j = i; j < matrix[0].size()-i-1; j++)
            {
                temp = matrix[i][j];
                matrix[i][j] = matrix[matrix[0].size()-1-j][i];
                matrix[matrix[0].size()-1-j][i] = matrix[matrix.size()-1-i][matrix.size()-1-j];
                matrix[matrix.size()-1-i][matrix.size()-1-j] =  matrix[j][matrix.size()-1-i];
                matrix[j][matrix.size()-1-i] = temp;
            }
        }
    }
};

 

posted on 2015-10-04 15:46  joannae  阅读(124)  评论(0编辑  收藏  举报

导航