-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRotateMatrix.java
More file actions
31 lines (28 loc) · 907 Bytes
/
RotateMatrix.java
File metadata and controls
31 lines (28 loc) · 907 Bytes
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
/**
* Created with IntelliJ IDEA.
* User: DavidAnderson
* Date: 11/11/13
* Time: 21:08
* To change this template use File | Settings | File Templates.
*/
public class RotateMatrix {
public static void rotate90Degrees(int[][] matrix){
int n = matrix.length;
for(int i = 0; i < n/2; i++){
doSwaps(matrix,i,n);
}
}
private static void doSwaps(int[][] matrix, int offset, int n){
int i = offset;
for(int j = offset; j< n-1-offset; j++){
swap(matrix, offset, j, j, n-1-offset);
swap(matrix, offset, j, n-1-offset, n-1-j);
swap(matrix, offset, j, n-1-j, offset);
}
}
private static void swap(int[][] matrix, int i1, int j1, int i2, int j2) {
int temp = matrix[i1][j1];
matrix[i1][j1] = matrix[i2][j2];
matrix[i2][j2] = temp;
}
}