Skip to content

Commit bb86b30

Browse files
committed
Code style fixes for matrix rotation algorithm.
1 parent 57378c5 commit bb86b30

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

‎src/algorithms/uncategorized/square-matrix-rotation/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ A B C
9292
/ / •
9393
/ • •
9494
95-
And not let's do horizontal reflection:
95+
And now let's do horizontal reflection:
9696
9797
A → →
9898
B → →
9999
C → →
100100
101-
The string has been rotated to 90 degree.
101+
The string has been rotated to 90 degree:
102102
103103
• • A
104104
• • B

‎src/algorithms/uncategorized/square-matrix-rotation/squareMatrixRotation.js‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,28 @@ export default function squareMatrixRotation(originalMatrix){
88
// Do top-right/bottom-left diagonal reflection of the matrix.
99
for(letrowIndex=0;rowIndex<matrix.length;rowIndex+=1){
1010
for(letcolumnIndex=rowIndex+1;columnIndex<matrix.length;columnIndex+=1){
11-
consttmp=matrix[columnIndex][rowIndex];
12-
matrix[columnIndex][rowIndex]=matrix[rowIndex][columnIndex];
13-
matrix[rowIndex][columnIndex]=tmp;
11+
// Swap elements.
12+
[
13+
matrix[columnIndex][rowIndex],
14+
matrix[rowIndex][columnIndex],
15+
]=[
16+
matrix[rowIndex][columnIndex],
17+
matrix[columnIndex][rowIndex],
18+
];
1419
}
1520
}
1621

1722
// Do horizontal reflection of the matrix.
1823
for(letrowIndex=0;rowIndex<matrix.length;rowIndex+=1){
1924
for(letcolumnIndex=0;columnIndex<matrix.length/2;columnIndex+=1){
20-
constmirrorColumnIndex=matrix.length-columnIndex-1;
21-
consttmp=matrix[rowIndex][mirrorColumnIndex];
22-
matrix[rowIndex][mirrorColumnIndex]=matrix[rowIndex][columnIndex];
23-
matrix[rowIndex][columnIndex]=tmp;
25+
// Swap elements.
26+
[
27+
matrix[rowIndex][matrix.length-columnIndex-1],
28+
matrix[rowIndex][columnIndex],
29+
]=[
30+
matrix[rowIndex][columnIndex],
31+
matrix[rowIndex][matrix.length-columnIndex-1],
32+
];
2433
}
2534
}
2635

0 commit comments

Comments
(0)