public class MatrixRotation {
//we have a n*n matrix
public static void rotate(int[][] matrix, int n){
int layer = n/2;
for(int i = 0; i < layer; i++){
int end = n - i - 2;
for(int j = i; j <= end; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[n - 1 - j][i];
matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = temp;
}
}
}
}

### Like this:

Like Loading...

*Related*

[…] Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a metho… […]