-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateMatrix.java
32 lines (24 loc) · 893 Bytes
/
RotateMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class RotateMatrix {
public void rotate(int[][] matrix) {
int l = 0;
int r = matrix.length - 1;
while ( l < r ) {
for(int i = 0; i < r - l; i++) {
int top = l;
int bottom = r;
//save the topleft
int topLeft = matrix[top][l + i];
//move bottom left into top left
matrix[top][l + i] = matrix[bottom - i][l];
// move bottom right into bottom left
matrix[bottom - i][l] = matrix[bottom][r - i];
// move top right into bottom right
matrix[bottom][r - i] = matrix[top + i][r];
// move top left into top right
matrix[top + i][r] = topLeft;
}
r -= 1;
l += 1;
}
}
}