本文介绍了旋转M * N矩阵(90度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何可以旋转矩阵
|3 4 5 6 8|
|5 4 3 2 6|
|3 3 7 8 9|
到
|8 6 9|
|6 2 8|
|5 3 7|
|4 4 3|
|3 5 3|
由于我见过的所有算法对于N * N矩阵。
Because all algorithms I've seen was for N*N matrix.
推荐答案
如果您的矩阵再由阵列psented $ P $ 矩阵[I,J]
,其中在我
的行和Ĵ
的列,然后实现下面的方法:
If your matrix is represented by an array matrix[i, j]
, where the i
are the rows and the j
are the columns, then implement the following method:
static int[,] RotateMatrixCounterClockwise(int[,] oldMatrix)
{
int[,] newMatrix = new int[oldMatrix.GetLength(1), oldMatrix.GetLength(0)];
int newColumn, newRow = 0;
for (int oldColumn = oldMatrix.GetLength(1) - 1; oldColumn >= 0; oldColumn--)
{
newColumn = 0;
for (int oldRow = 0; oldRow < oldMatrix.GetLength(0); oldRow++)
{
newMatrix[newRow, newColumn] = oldMatrix[oldRow, oldColumn];
newColumn++;
}
newRow++;
}
return newMatrix;
}
这适用于所有规模的矩阵。
This works for matrices of all sizes.
这篇关于旋转M * N矩阵(90度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!