rotateSquareImageCCW
to rotate the image counterclockwise - in-place. This problem can be broken down into simpler sub-problems you've already solved earlier! Rotating an image counterclockwise can be achieved by taking the transpose of the image matrix and then flipping it on its horizontal axis. int rows = matrix.length
and int columns = matrix[0].length
. In your learning program you may have come across the following sub-problems - Find the transpose of a square matrix and Flip an image on its horizontal axis. If we combine the two - we're done! transposeMatrix
+ flipItHorizontalAxis
. As a refresher, here's the pattern for getting the transpose of a matrix : for(int i = 0; i <= n; i++){
for(int j = i+1; j <= n; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
public static void rotateSquareImageCCW(int[][] matrix) { }
C
Java
Python