题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
import java.util.*; public class Solution { public int[][] rotateMatrix(int[][] mat, int n) { // write code here int up = 0; int bottom = mat.length - 1; while(up < bottom){ for(int i = 0 ; i < n ; i++){ swap(mat,up,i,bottom,i); } up++; bottom--; } for(int i = 0 ; i < n ; i++){ for(int j = i + 1 ; j < n ; j++){ swap(mat,i,j,j,i); } } return mat; } void swap(int [][]mat, int a1,int b1 ,int a2 ,int b2){ int temp = mat[a1][b1]; mat[a1][b1] = mat[a2][b2]; mat[a2][b2] = temp; } }