48. Rotate Image

  1. Rotate Image My Submissions QuestionEditorial Solution
    Total Accepted: 65856 Total Submissions: 190558 Difficulty: Medium
    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();
        if(n==0)return;
        for(int i=0;i<n;i++){
            for(int j=i;j<n;j++){
                swap(matrix[i][j],matrix[j][i]);
            }
        }
         for(int j=0;j*2<n-1;j++){
            for(int i=0;i<n;i++){
                swap(matrix[i][j],matrix[i][n-1-j]);
            }
        }
    }
};

你可能感兴趣的:(48. Rotate Image)