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?

矩阵的旋转

1 2 3

4 5 6

7 8 9

顺时针旋转90度为

7 4 1

8 5 2

9 6 3

(i,j)旋转后变成 (j,n-i-1)

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int n = matrix.size();
        for(int i = 0 ;i  < n/2; ++ i){
            for(int j = i; j < n - i-1; ++ j){
                swap(matrix[i][j],matrix[j][n-i-1]); 
                swap(matrix[i][j],matrix[n-i-1][n-j-1]);
                swap(matrix[i][j],matrix[n-j-1][i]);
            }
        }
    }
};
原文地址:https://www.cnblogs.com/xiongqiangcs/p/3815243.html