Fork me on GitHub

leetcode-48-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?
思路:先翻转再对换
1 2 3    7 8 9  7 4 1
4 5 6 =>  4 5 6 => 8 5 2
7 8 9    1 2 3   9 6 3

void rotate(vector<vector<int>>& matrix)
    {
        reverse(matrix.begin(), matrix.end());
        for (int i = 0; i < matrix.size(); ++i)
            for (int j = 0; j < i; ++j)
               swap(matrix[i][j], matrix[j][i]);
    }

参考:

http://blog.csdn.net/makuiyu/article/details/44034561

posted @ 2017-04-07 09:35  hellowOOOrld  阅读(105)  评论(0)    收藏  举报