mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Code style fixes for matrix rotation algorithm.
This commit is contained in:
parent
57378c5e19
commit
bb86b30dda
@ -92,13 +92,13 @@ A B C
|
|||||||
/ / •
|
/ / •
|
||||||
/ • •
|
/ • •
|
||||||
|
|
||||||
And not let's do horizontal reflection:
|
And now let's do horizontal reflection:
|
||||||
|
|
||||||
A → →
|
A → →
|
||||||
B → →
|
B → →
|
||||||
C → →
|
C → →
|
||||||
|
|
||||||
The string has been rotated to 90 degree.
|
The string has been rotated to 90 degree:
|
||||||
|
|
||||||
• • A
|
• • A
|
||||||
• • B
|
• • B
|
||||||
|
@ -8,19 +8,28 @@ export default function squareMatrixRotation(originalMatrix) {
|
|||||||
// Do top-right/bottom-left diagonal reflection of the matrix.
|
// Do top-right/bottom-left diagonal reflection of the matrix.
|
||||||
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) {
|
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) {
|
||||||
for (let columnIndex = rowIndex + 1; columnIndex < matrix.length; columnIndex += 1) {
|
for (let columnIndex = rowIndex + 1; columnIndex < matrix.length; columnIndex += 1) {
|
||||||
const tmp = matrix[columnIndex][rowIndex];
|
// Swap elements.
|
||||||
matrix[columnIndex][rowIndex] = matrix[rowIndex][columnIndex];
|
[
|
||||||
matrix[rowIndex][columnIndex] = tmp;
|
matrix[columnIndex][rowIndex],
|
||||||
|
matrix[rowIndex][columnIndex],
|
||||||
|
] = [
|
||||||
|
matrix[rowIndex][columnIndex],
|
||||||
|
matrix[columnIndex][rowIndex],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do horizontal reflection of the matrix.
|
// Do horizontal reflection of the matrix.
|
||||||
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) {
|
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) {
|
||||||
for (let columnIndex = 0; columnIndex < matrix.length / 2; columnIndex += 1) {
|
for (let columnIndex = 0; columnIndex < matrix.length / 2; columnIndex += 1) {
|
||||||
const mirrorColumnIndex = matrix.length - columnIndex - 1;
|
// Swap elements.
|
||||||
const tmp = matrix[rowIndex][mirrorColumnIndex];
|
[
|
||||||
matrix[rowIndex][mirrorColumnIndex] = matrix[rowIndex][columnIndex];
|
matrix[rowIndex][matrix.length - columnIndex - 1],
|
||||||
matrix[rowIndex][columnIndex] = tmp;
|
matrix[rowIndex][columnIndex],
|
||||||
|
] = [
|
||||||
|
matrix[rowIndex][columnIndex],
|
||||||
|
matrix[rowIndex][matrix.length - columnIndex - 1],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user