有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵。
数据范围:0 < n < 3000 要求:空间复杂度O(n^2),时间复杂度 O(n^2) 找规律题,很简单:
进阶:空间复杂度 O(1),时间复杂度 O(n^2)
输入:
[[1,2,3],[4,5,6],[7,8,9]],3
返回值:
[[7,4,1],[8,5,2],[9,6,3]]import java.util.*;
public class Solution {
public int[][] rotateMatrix1(int[][] mat, int n) {
// write code here
int[][] temp = new int[n][n];//新建temp对象,作为最终返回的对象
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
temp[j][n-1-i] = mat[i][j];//直接交换
}
}
return temp;
}
public int[][] rotateMatrix(int[][] mat, int n) {
// write code here
//由外而内,一层一层地进行变换
int rotateTimes=n/2;
int i=0,low=0,high=n-1;
while(i++



