顺时针旋转矩阵
作者:互联网
链接
有一个NxN整数矩阵,请编写一个算法,将矩阵顺时针旋转90度。
给定一个NxN的矩阵,和矩阵的阶数N,请返回旋转后的NxN矩阵。
import java.util.*;
public class Solution {
public int[][] rotateMatrix(int[][] mat, int n) {
if (n <= 1) {
return mat;
}
int row1 = 0, col1 = 0, row2 = n - 1, col2 = n - 1;
while (row1 < row2 && col1 < col2) {
for (int i = col1; i < col2; ++i) {
int distance = i - col1;
int tmp = mat[row1][i];
mat[row1][i] = mat[row2 - distance][col1];
mat[row2 - distance][col1] = mat[row2][col2 - distance];
mat[row2][col2 - distance] = mat[row1 + distance][col2];
mat[row1 + distance][col2] = tmp;
}
row1++;
col1++;
row2--;
col2--;
}
return mat;
}
}
标签:顺时针,int,矩阵,旋转,NxN,public 来源: https://www.cnblogs.com/tianyiya/p/15399988.html