Share my CPP soluation, O(1) space,10 lines code


  • 1
    C

    Just turn, turn and turn ....

    void rotate(vector<vector<int> >& matrix) 
    {
        int n = matrix.size();
        for(int s=0,e=n-1; e>=s; s++,e--)
        {
            for(int i = s; i < e; ++i)
            {
                int tmp = matrix[i][e];
                matrix[i][e] = matrix[s][i];
                matrix[s][i] = matrix[e-i+s][s];
                matrix[e-i+s][s] = matrix[e][e-i+s];
                matrix[e][e-i+s] = tmp;
            }
        }
    
    }

Log in to reply
 

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