Leetcode: 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?

 

update

先转置, 后按列翻转

 

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        transpose(matrix);

        for(int i = 0; i < matrix.size(); i ++)
            reverse(matrix[i].begin(), matrix[i].end());

    }

    void transpose(vector<vector<int> > &matrix) {
        int n = matrix.size();

        for(int i = 0; i < n; i ++) {
            for(int j = i; j < n; j ++) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }
    }
};

 

 

 

posted @ 2014-01-08 14:56  SangS  阅读(141)  评论(0编辑  收藏  举报