LeetCode/com/zerroi/leetcode/Three17/Rotate.java

36 lines
984 B
Java
Raw Permalink Normal View History

2024-03-21 19:03:41 +08:00
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);
}
}
}