大佬教程收集整理的这篇文章主要介绍了旋转M * N矩阵(90度),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果矩阵是由array表示的@H_957_3@matrix[i, j],其中的i
是行,而的j
是列,则请实施以下方法:
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;
}
这适用于所有大小的矩阵。
:如果此操作太昂贵,则可以尝试更改 读取 矩阵的方式,而不是更改矩阵 本身 。例如,如果我按如下所示显示矩阵:
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); coL++)
{
Console.Write(matrix[row, col] + " ");
}
Console.Writeline();
}
那么我可以通过更改读取矩阵的方式来表示逆时针旋转90度:
for (int col = matrix.GetLength(1) - 1; col >= 0; col--)
{
for (int row = 0; row < matrix.GetLength(0); row++)
{
Console.Write(matrix[row, col] + " ");
}
Console.Writeline();
}
该访问模式也可以抽象为一个类。
如何旋转矩阵
|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矩阵的。
以上是大佬教程为你收集整理的旋转M * N矩阵(90度)全部内容,希望文章能够帮你解决旋转M * N矩阵(90度)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。