C++ 4ms solution


  • 1
    Y
    class Solution {
    public:
        void rotate(vector<vector<int>>& matrix) {
            transpose(matrix);
            flip(matrix);
        }
    
        void transpose(vector<vector<int>>& matrix) {
        	if (matrix.empty())
        		return;
        	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]);
        }
    
        void flip(vector<vector<int>>& matrix) {
        	if (matrix.empty())
        		return;
        	int n = matrix.size();
        	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.