Mid Coding

Rotate a matrix by 90 degrees in place?

public void RotateMatrix(int[][] matrix) {

int n = matrix.Length;

// Transpose

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;

// Reverse each row

for (int i = 0; i < n; i++) {

Array.Reverse(matrix[i]);

Explanation:

Transpose matrix and then reverse each row to rotate 90° clockwise.

More from C# Programming Tutorial

All questions for this course