rotate image


Problem Description
这题按照CC150上的做法,我是死活写不对。幸好大牛给了条活路,先把矩阵上下行交换,然后,沿着对角线交换
图片引用自:http://fisherlei.blogspot.com/2013/01/leetcode-rotate-image.html

image

略微有点不一样,结果都一样的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void (int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0)
return;
int n = matrix.length - 1;

for (int i = 0; i < matrix.length / 2 ; ++i) {
for (int j = 0; j < matrix[0].length; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - i][j];
matrix[n - i][j] = temp;
}
}
// symmetry swap
for (int i = 0; i < matrix.length; ++i) {
for (int j = i + 1; j < matrix[0].length; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}