顺时针旋转90度等于先转置再左右翻转
顺时针旋转180度等于先上下翻转再左右翻转
顺时针旋转270度等于先左右翻转再转置
package leetcode; public class demo_48 { public void rotate(int[][] matrix) { //顺时针旋转90度等于先转置再对称翻转 for(int i=0;i<matrix.length;i++) { for(int j=i+1;j<matrix.length;j++) { int t=matrix[i][j]; matrix[i][j]=matrix[j][i]; matrix[j][i]=t; } } for(int i=0;i<matrix.length;i++) { for(int j=0;j<matrix.length/2;j++) { int t=matrix[i][j]; matrix[i][j]=matrix[i][matrix.length-1-j]; matrix[i][matrix.length-1-j]=t; } } for(int[] s:matrix) { for(int i:s) { System.out.print(i+" "); } } } public static void main(String[] args) { // TODO Auto-generated method stub demo_48 d48=new demo_48(); int[][]matrix= {{1,2,3},{4,5,6},{7,8,9}}; d48.rotate(matrix); } }