4ms C++ solution using reverse and swap in 9 lines


  • 0
    W

    {

    void rotate(vector<vector<int>>& matrix) {
        
        int matrixSize = matrix.size();
        if(!matrixSize) return;
        
        for(int i = 0; i < matrixSize; i++)
        {   reverse(matrix[i].begin(), matrix[i].end());    }
        
        for(int i = 0; i < matrixSize; i++ )
        {
            for(int j = 0; j < (matrixSize - i - 1); j++)
            {   swap(matrix[i][j], matrix[matrixSize - j - 1][matrixSize - i - 1]); }
        }
    }
    

    }


Log in to reply
 

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