4ms c++ in place solution


  • 4
    I
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for (int i = 0; i < n; i++) {
    	    for (int j = 0; j < n / 2; j++) {
    		    swap(matrix[i][j], matrix[i][n - 1 - j]);
    	    }
        }
        for (int i = 0; i < n - 1; i++) {
    	    for (int j = 0; j < n - i - 1; j++) {
    		    swap(matrix[i][j], matrix[n - 1 - j][n - 1 - i]);
    	    }
        }
    }

Log in to reply
 

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