Skip to content

Commit

Permalink
Code style fixes for matrix rotation algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Jul 6, 2018
1 parent 57378c5 commit bb86b30
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/algorithms/uncategorized/square-matrix-rotation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ A B C
/ / •
/ • •
And not let's do horizontal reflection:
And now let's do horizontal reflection:
A → →
B → →
C → →
The string has been rotated to 90 degree.
The string has been rotated to 90 degree:
• • A
• • B
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ export default function squareMatrixRotation(originalMatrix) {
// Do top-right/bottom-left diagonal reflection of the matrix.
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) {
for (let columnIndex = rowIndex + 1; columnIndex < matrix.length; columnIndex += 1) {
const tmp = matrix[columnIndex][rowIndex];
matrix[columnIndex][rowIndex] = matrix[rowIndex][columnIndex];
matrix[rowIndex][columnIndex] = tmp;
// Swap elements.
[
matrix[columnIndex][rowIndex],
matrix[rowIndex][columnIndex],
] = [
matrix[rowIndex][columnIndex],
matrix[columnIndex][rowIndex],
];
}
}

// Do horizontal reflection of the matrix.
for (let rowIndex = 0; rowIndex < matrix.length; rowIndex += 1) {
for (let columnIndex = 0; columnIndex < matrix.length / 2; columnIndex += 1) {
const mirrorColumnIndex = matrix.length - columnIndex - 1;
const tmp = matrix[rowIndex][mirrorColumnIndex];
matrix[rowIndex][mirrorColumnIndex] = matrix[rowIndex][columnIndex];
matrix[rowIndex][columnIndex] = tmp;
// Swap elements.
[
matrix[rowIndex][matrix.length - columnIndex - 1],
matrix[rowIndex][columnIndex],
] = [
matrix[rowIndex][columnIndex],
matrix[rowIndex][matrix.length - columnIndex - 1],
];
}
}

Expand Down

0 comments on commit bb86b30

Please sign in to comment.