题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
观察矩阵外圈,宏观想像
import java.util.*;
public class Solution {
public int[][] rotateMatrix(int[][] mat, int n) {
// write code here
int x1 = 0, y1 = 0;
int x2 = mat.length - 1, y2 = mat[0].length - 1;
while (x1 <= x2 && y1 <= y2) {
rotateEdge(mat, x1++, y1++, x2--, y2--);
}
return mat;
}
public void rotateEdge(int[][] m, int x1, int y1, int x2, int y2) {
int temp = 0;
for (int i = 0; i < x2 - x1; i++) {
temp = m[x1][y1 + i];
m[x1][y1 + i] = m[x2 - i][y1];
m[x2 - i][y1] = m[x2][y2 - i];
m[x2][y2 - i] = m[x1 + i][y2];
m[x1 + i][y2] = temp;
}
}
}