Share my 6ms C++ code(in place)


  • 0
    M
    class Solution {
    public:
        void rotate(vector<vector<int> > &matrix) {
            int n=matrix.size();
            if(n<=1){
                return;
            }
            for(int i=0;i<n-1;++i){
                for(int j=0;j<n-1-i;++j){
                    exchange(matrix[i][j],matrix[n-1-j][n-1-i]);
                }
            }
            for(int i=0;i<n;++i){
                int up=0;
                int down=n-1;
                while(up<down){
                    exchange(matrix[up][i],matrix[down][i]);
                    up++;
                    down--;
                }
            }
        }
        void exchange(int &a,int &b){
            a=a^b;
            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.