[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?

思考:两步,先对角线交换再上下交换。

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int n=matrix.size();
        int i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n-i;j++)
            {
                swap(matrix[i][j],matrix[n-1-j][n-1-i]);
            }
        }
        for(i=0;i<n/2;i++)
        {
            for(j=0;j<n;j++)
            {
                swap(matrix[i][j],matrix[n-1-i][j]);
            }
        }
    }
};

  

原文地址:https://www.cnblogs.com/Rosanna/p/3473790.html