c++ easy to understand solution 3ms


  • 0
    J
    public:
        void rotate(vector<vector<int>>& matrix) {
            //first transpose the matrix
            int n = matrix.size();
            for(int i = 0;i < n;i++)
                for(int j = i + 1;j < n;j++)
                    swap(matrix[i][j], matrix[j][i]);
            //then mirror the matrix by the vertical mid line
            for(int i = 0;i < n;i++)
        	    for(int j = 0;j < n / 2;j++)
        		    swap(matrix[i][j], matrix[i][n - 1 - j]);
        }
    };

Log in to reply
 

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