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?
不借助辅助空间顺时针旋转二维数组90度

思路

先沿副对角线翻转,再沿横中轴线翻转

代码

class Solution {
public:
    void rotate(vector<vector<int> >& matrix) {
        int n = matrix.size();
        //沿副对角线翻转
        for(int i=0; ifor(int j=0; j1-i; j++)
                swap(matrix[i][j], matrix[n-1-j][n-1-i]);
        //沿横横中轴线翻转
        for(int i=0; ifor(int j=0; j2; j++)
                swap(matrix[j][i], matrix[n-j-1][i]);
    }
};

你可能感兴趣的:(数据结构及算法,二维数组,matrix,c++,leetcode)