36 lines
984 B
Java
36 lines
984 B
Java
package com.zerroi.leetcode.Three17;
|
||
|
||
import java.util.Arrays;
|
||
|
||
public class Rotate {
|
||
public static void main(String[] args) {
|
||
int[][] matrix = new int[][]{{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}};
|
||
SolutionFirst solutionFirst = new SolutionFirst();
|
||
solutionFirst.rotate(matrix);
|
||
}
|
||
}
|
||
/*
|
||
* 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。
|
||
|
||
* */
|
||
class SolutionFirst {
|
||
public void rotate(int[][] matrix) {
|
||
int n = matrix.length;
|
||
int[][] res = new int[n][n];
|
||
int indexCol;
|
||
int indexRow = 0;
|
||
for (int i = n - 1; i >= 0; i--) {
|
||
indexCol = n - 1;
|
||
for (int j = n - 1; j >= 0; j--) {
|
||
res[indexCol][indexRow] = matrix[i][j];
|
||
indexCol--;
|
||
}
|
||
indexRow++;
|
||
}
|
||
|
||
for (int i = 0; i < n; i++) {
|
||
System.arraycopy(res[i], 0, matrix[i], 0, n);
|
||
}
|
||
}
|
||
}
|