Leetcode 48.旋转矩阵

旋转矩阵

给定一个 × n 的二维矩阵表示一个图像。

将图像顺时针旋转 90 度。

说明:

你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。

示例 1:

给定 matrix =

[

[1,2,3],

[4,5,6],

[7,8,9]

],

原地旋转输入矩阵,使其变为:

[

[7,4,1],

[8,5,2],

[9,6,3]

]

 1 import java.util.*;
 2 public class Rotate {
 3     public int[][] rotateMatrix(int[][] mat, int n) {
 4         // write code here
 5         int[][] temp=new int[n][n];
 6         for(int i=0;i<n;i++)
 7         {
 8             for(int j=0;j<n;j++)
 9             {
10                 temp[j][n-1-i]=mat[i][j];
11             }
12         }
13         return temp;
14     }
15 }


原文地址:https://www.cnblogs.com/kexinxin/p/10163025.html