-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateImage.java
More file actions
40 lines (34 loc) · 1022 Bytes
/
Copy pathRotateImage.java
File metadata and controls
40 lines (34 loc) · 1022 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
package array;
/**
* Created by qiaoying on 2018/7/1.
*/
public class RotateImage {
public static void rotate(int[][] matrix) {
int length = matrix.length;
// 调换对角元素
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - i; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[length - j - 1][length - i - 1];
matrix[length - j - 1][length - i - 1] = tmp;
}
}
// 调换列元素
for (int i = 0; i < length; i++) {
for (int j = 0; j < length / 2; j++) {
int tmp = matrix[j][i];
matrix[j][i] = matrix[length - j - 1][i];
matrix[length - j - 1][i] = tmp;
}
}
}
public static void main(String[] args){
int[][] a = {
{ 5, 1, 9,11},
{2, 4, 8,10},
{13, 3, 6, 7},
{15,14,12,16}
};
rotate(a);
}
}