Rotate Matrix by 90 Degrees
Understanding the Problem
The goal is to rotate a given square matrix by 90 degrees clockwise.
Method 1: Using a Temporary Matrix
This method uses a temporary matrix to store the rotated values.
public class RotateMatrix { public static void rotateMatrix(int[][] matrix) { int n = matrix.length; int[][] temp = new int[n][n]; // Store the rotated values in a temporary matrix for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { temp[j][n - 1 - i] = matrix[i][j]; } } // Copy the temporary matrix back to the original matrix for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i][j] = temp[i][j]; } } } public static void printMatrix(int[][] matrix) { for (int[] row : matrix) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } } public static void main(String[] args) { int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotateMatrix(matrix); System.out.println("Rotated Matrix:"); printMatrix(matrix); } }
Output:
Rotated Matrix: 7 4 1 8 5 2 9 6 3
Method 2: In-Place Rotation
This method rotates the matrix in place without using extra space.
public class RotateMatrix { public static void rotateMatrix(int[][] matrix) { int n = matrix.length; // Transpose the matrix for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } // Reverse each row for (int i = 0; i < n; i++) { for (int j = 0; j < n / 2; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[i][n - 1 - j]; matrix[i][n - 1 - j] = temp; } } } public static void printMatrix(int[][] matrix) { for (int[] row : matrix) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } } public static void main(String[] args) { int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotateMatrix(matrix); System.out.println("Rotated Matrix:"); printMatrix(matrix); } }
Output:
Rotated Matrix: 7 4 1 8 5 2 9 6 3
Method 3: Using Layer by Layer Rotation
This method rotates the matrix layer by layer.
public class RotateMatrix { public static void rotateMatrix(int[][] matrix) { int n = matrix.length; for (int layer = 0; layer < n / 2; layer++) { int first = layer; int last = n - 1 - layer; for (int i = first; i < last; i++) { int offset = i - first; // Save the top element int top = matrix[first][i]; // Move left element to top matrix[first][i] = matrix[last - offset][first]; // Move bottom element to left matrix[last - offset][first] = matrix[last][last - offset]; // Move right element to bottom matrix[last][last - offset] = matrix[i][last]; // Assign top element to right matrix[i][last] = top; } } } public static void printMatrix(int[][] matrix) { for (int[] row : matrix) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } } public static void main(String[] args) { int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; rotateMatrix(matrix); System.out.println("Rotated Matrix:"); printMatrix(matrix); } }
Output:
Rotated Matrix: 7 4 1 8 5 2 9 6 3