题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
http://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
import java.util.*;
public class Solution {
public int[][] rotateMatrix(int[][] mat, int n) {
// write code here
if(n==0){
return mat;
}
//水平翻转,在沿主对角线翻转
for(int i=0,k=n/2;i<k;i++){
for(int j=0;j<n;j++){
swap(mat,i,j,n-i-1,j);
}
}
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
swap(mat,i,j,j,i);
}
}
return mat;
}
private void swap(int[][] a,int row,int col,int row1,int col1){
int temp=a[row][col];
a[row][col]=a[row1][col1];
a[row1][col1]=temp;
}
}