描述
有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵。
数据范围:0 < n < 300,矩阵中的值满足0≤val≤1000
要求:空间复杂度 O(n^2),时间复杂度 O(n^2)
题解:将矩阵看成是一个一个列向量,进行翻转可得。
public class Solution {
public int[][] rotateMatrix(int[][] mat, int n) {
// write code here
int [] [] num=new int [n] [n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
num[i][n-j-1]=mat[j][i];
}
}
return num;
}
}