题解 | 顺时针旋转矩阵
顺时针旋转矩阵
https://www.nowcoder.com/practice/2e95333fbdd4451395066957e24909cc
import java.util.*;
public class Solution {
/**
* 1 2 3
* 4 5 6
* 7 8 9
* <p>
* 7 4 1
* 8 5 2
* 9 6 3
* <p>
* 旋转矩阵,实际是将(i,j)位置的元素放到了新位置(j,n-1-i)
*/
public int[][] rotateMatrix(int[][] mat, int n) {
int[][] newMat = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newMat[j][n - 1 - i] = mat[i][j];
}
}
return newMat;
}
}
滴滴公司福利 1815人发布