-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_48RotateImage.java
More file actions
41 lines (37 loc) · 983 Bytes
/
Copy path_48RotateImage.java
File metadata and controls
41 lines (37 loc) · 983 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
32
33
34
35
36
37
38
39
40
41
package leetcode;
import java.util.Arrays;
public class _48RotateImage {
public static void main(String[] args) {
int[][] a = {
{1,2,3},
{4,5,6},
{7,8,9} };
rotate(a);
for (int i = 0; i < a.length; i++) {
System.out.println(Arrays.toString(a[i]));
}
}
public static void rotate(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0] == null
|| matrix[0].length == 0)
return;
int n = matrix.length;
int border = n >> 1;
for (int i = 0; i < border; i++) {
int[] t = matrix[i];
matrix[i] = matrix[n - 1 - i];
matrix[n - 1 - i] = t;
}
// for (int i = 0; i < matrix.length; i++) {
// System.out.println(Arrays.toString(matrix[i]));
// }
// 转置
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
int t = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = t;
}
}
}
}