inplace solution, one round for loop.


  • 0
    Y

    '''

    class Solution {
    public:

    void rotate(vector<vector<int>>& matrix) {
        if (matrix.empty()) return;
        for (size_t i = 0; i <= (matrix.size() - 1) / 2; ++i)
            for (size_t j = i; j < matrix.size() - 1 - i; ++j) {
                int i1 = i, j1 = j;
                int i2 = j, j2 = matrix.size() - i - 1;
                int i3 = matrix.size() - 1 - i, j3 = matrix.size() - 1 - j;
                int i4 = matrix.size() - 1 - j, j4 = i;
    
                swap(matrix[i1][j1], matrix[i4][j4]);
    
                swap(matrix[i2][j2], matrix[i4][j4]);
    
                swap(matrix[i3][j3], matrix[i4][j4]);
    
            }
    }
    

    };
    '''


Log in to reply
 

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