-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (32 loc) · 1.25 KB
/
Solution.java
File metadata and controls
39 lines (32 loc) · 1.25 KB
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
33
34
35
36
37
38
39
public class Solution {
// public void rotate(int[][] matrix) {
// if (matrix == null || matrix.length == 0 || matrix.length != matrix[0].length)
// return;
// int n = matrix.length;
// int[][] bak = Arrays.copyOf(matrix, n);
// for (int i = 0; i < n; i++) {
// bak[i] = Arrays.copyOf(matrix[i], n);
// }
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// matrix[i][j] = bak[n-1-j][i];
// }
// }
// }
//rotate in-place
public void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix.length != matrix[0].length)
return;
int endLine = matrix.length >> 1;
int lastIndex = matrix.length - 1;
for (int i = 0; i < endLine; i++) {
for (int j = i; j < lastIndex-i; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[lastIndex-j][i];
matrix[lastIndex-j][i] = matrix[lastIndex-i][lastIndex-j];
matrix[lastIndex-i][lastIndex-j] = matrix[j][lastIndex-i];
matrix[j][lastIndex-i] = tmp;
}
}
}
}