Skip to content

Commit 56d2949

Browse files
refactor 48
1 parent c6544b2 commit 56d2949

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/main/java/com/fishercoder/solutions/_48.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
public class _48 {
66

7-
/**Note: this is an n*n matrix, in other words, it's a square, this makes it easier as well.*/
7+
/**
8+
* Note: this is an n*n matrix, in other words, it's a square, this makes it easier as well.
9+
*/
810

911
public static class Solution1 {
1012
//Time: O(n^2)
@@ -42,12 +44,13 @@ public void rotate(int[][] matrix) {
4244
}
4345

4446
public static class Solution2 {
45-
/**First swap the rows bottom up, then swap the element on the diagonal:
46-
*
47+
/**
48+
* First swap the rows bottom up, then swap the element on the diagonal:
49+
* <p>
4750
* 1, 2, 3 7, 8, 9 7, 4, 1
4851
* 4, 5, 6 becomes 4, 5, 6 becomes 8, 5, 2
4952
* 7, 8, 9 1, 2, 3 9, 6, 3
50-
*
53+
* <p>
5154
* Time: O(n^2)
5255
* Space: O(1)
5356
*/
@@ -75,23 +78,43 @@ public void rotate(int[][] matrix) {
7578
}
7679

7780
public static class Solution3 {
81+
/**
82+
* You only need to rotate the top right quarter,
83+
* with this example:
84+
* {1, 2, 3, 4},
85+
* {5, 6, 7, 8},
86+
* {9, 10, 11, 12},
87+
* {13, 14, 15, 16}
88+
*
89+
* top will only be:
90+
* 1, 2, 3,
91+
* 6
92+
*
93+
* then this entire matrix is rotated. As they'll drive to ratate the corresponding three elements in the matrix.
94+
*
95+
* Another cool trick that this solution takes advantage is that because it's a square matrix,
96+
* meaning number of rows equals to the number of columns, we could do swap like this,
97+
* if it's a rectangular, below method won't work and will throw ArrayIndexOutOfBoundsException.
98+
*
99+
*/
78100
public void rotate(int[][] matrix) {
79101
int n = matrix.length;
80102
for (int i = 0; i < n / 2; i++) {
81103
for (int j = i; j < n - i - 1; j++) {
82-
//save the top
104+
//save the top left
83105
int top = matrix[i][j];
106+
System.out.println("top = " + top);
84107

85-
//move left to top
108+
//move bottom left to top left
86109
matrix[i][j] = matrix[n - 1 - j][i];
87110

88-
//move bottom to left
111+
//move bottom right to bottom left
89112
matrix[n - 1 - j][i] = matrix[n - i - 1][n - 1 - j];
90113

91-
//move right to bottom
114+
//move top right to bottom right
92115
matrix[n - i - 1][n - 1 - j] = matrix[j][n - i - 1];
93116

94-
//move top to right
117+
//move top left to top right
95118
matrix[j][n - i - 1] = top;
96119
}
97120
}

src/test/java/com/fishercoder/_48Test.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void test5() {
7070
{9, 10, 11, 12},
7171
{13, 14, 15, 16}
7272
};
73+
CommonUtils.print2DIntArray(matrix);
7374
solution3.rotate(matrix);
7475
CommonUtils.print2DIntArray(matrix);
7576
}
@@ -85,4 +86,5 @@ public void test6() {
8586
solution1.rotate(matrix);
8687
CommonUtils.print2DIntArray(matrix);
8788
}
89+
8890
}

0 commit comments

Comments
 (0)