function rotate(matrix) {
const n = matrix.length;
// Step 1: Transpose the matrix
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
// Swap matrix[i][j] and matrix[j][i]
const temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Step 2: Reverse each row
for (let i = 0; i < n; i++) {
matrix[i].reverse();
}
}
// 示例
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
rotate(matrix);
console.log(matrix);
10-27
2265
05-14
580
12-02
403