File tree 2 files changed +38
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -80,4 +80,28 @@ public void rotate(int[][] matrix) {
80
80
}
81
81
}
82
82
83
+ public static class Solution3 {
84
+ public void rotate (int [][] matrix ) {
85
+ int n = matrix .length ;
86
+ for (int i = 0 ; i < n / 2 ; i ++) {
87
+ for (int j = i ; j < n - i - 1 ; j ++) {
88
+ //save the top
89
+ int top = matrix [i ][j ];
90
+
91
+ //move left to top
92
+ matrix [i ][j ] = matrix [n - 1 - j ][i ];
93
+
94
+ //move bottom to left
95
+ matrix [n - 1 - j ][i ] = matrix [n - i - 1 ][n - 1 - j ];
96
+
97
+ //move right to bottom
98
+ matrix [n - i - 1 ][n - 1 - j ] = matrix [j ][n - i - 1 ];
99
+
100
+ //move top to right
101
+ matrix [j ][n - i - 1 ] = top ;
102
+ }
103
+ }
104
+ }
105
+ }
106
+
83
107
}
Original file line number Diff line number Diff line change 8
8
public class _48Test {
9
9
private static _48 .Solution1 solution1 ;
10
10
private static _48 .Solution2 solution2 ;
11
+ private static _48 .Solution3 solution3 ;
11
12
private static int [][] matrix ;
12
13
13
14
@ BeforeClass
14
15
public static void setup () {
15
16
solution1 = new _48 .Solution1 ();
16
17
solution2 = new _48 .Solution2 ();
18
+ solution3 = new _48 .Solution3 ();
17
19
}
18
20
19
21
@ Test
@@ -59,4 +61,16 @@ public void test4() {
59
61
solution1 .rotate (matrix );
60
62
CommonUtils .print2DIntArray (matrix );
61
63
}
64
+
65
+ @ Test
66
+ public void test5 () {
67
+ matrix = new int [][]{
68
+ {1 , 2 , 3 , 4 },
69
+ {5 , 6 , 7 , 8 },
70
+ {9 , 10 , 11 , 12 },
71
+ {13 , 14 , 15 , 16 }
72
+ };
73
+ solution3 .rotate (matrix );
74
+ CommonUtils .print2DIntArray (matrix );
75
+ }
62
76
}
You can’t perform that action at this time.
0 commit comments