Simple C++ solution. Two inplace mirror operations.


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.