题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
public class Solution {
//基本要求:增加空间复杂度
public int[][] rotateMatrix_base(int[][] mat, int n) {
int[][] rotate = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
rotate[j][n-1-i] = mat[i][j];
}
}
return rotate;
}
//进阶:直接在原矩阵上修改
public int[][] rotateMatrix(int[][] mat, int n) {
//对角翻转
for(int i=0; i<n; i++){
for(int j=0; j<i; j++){
int tmp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = tmp;
}
}
//左右翻转
for(int i=0; i<n; i++){
for(int j=0; j<n/2; j++){
int tmp = mat[i][j];
mat[i][j] = mat[i][n-1-j];
mat[i][n-1-j] = tmp;
}
}
return mat;
}
}