首页 > 代码库 > LeetCode:Rotate Image

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?


思路:先将行顺序reverse,然后再对每个处于下三角区域的元素rotate。


代码:

void Solution::rotate(vector<vector<int> > &matrix)
{

    reverse(matrix.begin(),matrix.end());
    for(int i = 0;i < matrix.size();i++)
        for(int j = 0;j < matrix.size() - i;j++)
        {
            int temp = matrix[j][i];
            matrix[j][i] = matrix[i][j];
            matrix[i][j] = temp;
        }
}


LeetCode:Rotate Image