Share my solution - C++


  • 3
    K

    Share my solution.

    class Solution {
    public:
        void rotate(vector<vector<int> > &matrix) {
            int n = matrix.size();
            if(n<=1) return;
            for(int i = 0; i!=n/2;i++)
            {
                for(int j = i;j!=n-1-i;j++)
                {
                swap(matrix[i][j],matrix[j][n-1-i]);
                swap(matrix[n-1-j][i],matrix[i][j]);
                swap(matrix[n-1-i][n-1-j],matrix[n-1-j][i]);
                }
            }
        }
        inline void swap(int &a,int &b)
        {
            a = b+a;
            b = a-b;
            a = a-b;
        }
    };

Log in to reply
 

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