题解 | #顺时针旋转矩阵#
顺时针旋转矩阵
https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param mat int整型二维数组 * @param n int整型 * @return int整型二维数组 */ public int[][] rotateMatrix (int[][] mat, int n) { // write code here //1. 先矩阵转置 for(int i=0; i< n; i++){ for(int j=0; j<i; j++){ int temp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = temp; } } //2. 再翻转 for(int i=0; i< n; i++){ for(int j=0; j<n/2; j++){ int temp = mat[i][j]; mat[i][j] = mat[i][n-j-1]; mat[i][n-j-1] = temp; } } return mat; } }