规律:假设某个元素在原始矩阵的位置为(i,j),那么这个元素在旋转后的矩阵的位置是倒数第j行,倒数第i列。详细分析见
public int[][] rotateMatrix(int[][] mat, int n) {
// write code here
ArrayList<int[]> temp = new ArrayList<>();
for(int i = 0; i < n; i++) {
temp.add(0,mat[i]);
}
int[][] res = new int[n][n];
for(int i = 0; i < temp.size(); i++) {
int[] r = temp.get(i);
for(int j = 0; j < n; j++) {
res[j][i] = r[j];
}
}
return res;
}
相关规律:
顺时针旋转90度:先将矩阵按照行上下交换,然后再将矩阵按照主对角线两两交换位置。
顺时针旋转180度:先将矩阵按照行上下交换,然后再按照列左右交换。
顺时针旋转270度(逆时针90度):先按照列左右交换,然后按照主对角线两两交换位置。
因篇幅问题不能全部显示,请点此查看更多更全内容