Jack Li's Blog

0048.Rotate Image

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        // row column to column row
        // left to right

        int m = matrix.size();
        
        for(int i = 0; i < m; i++){
            for(int j = i; j < m; j++) {
                swap(matrix[i][j], matrix[j][i]);
            }
        }

        for(int i = 0; i < m; i++){
            for(int j = 0; j < m/2; j++){
                swap(matrix[i][j], matrix[i][m-j-1]);
            }
        }
    }
};