package com.zmz.algorithm.array;
/**
* @author 张明泽
* Create by 2022/5/20 11:03
* 旋转矩阵
* LeetCode-48
*/
public class RotationMatrix {
public static void main(String[] args) {
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
// 转置 只需要对角线的一半
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < i; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
// 镜像 y轴方向的一半
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length >>> 1; j++) {
int temp = arr[i][arr.length - 1 -j];
arr[i][arr.length - 1 -j] = arr[i][j];
arr[i][j] = temp;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}
最后修改:2022 年 05 月 25 日 02 : 04 PM
© 允许规范转载