public class Rotate
{
public static int[][] rotateArray(int[][] orig)
{
int[][] neo = new int[orig.length][orig[0].length];
for(int r = 0; r < orig.length; r++)
{
for(int c = 0; c < orig[0].length; c++)
{
neo[(orig.length - 1) - c][r] = orig[r][c];
}
}
return neo;
}
}
这个问题以前已经回答过,但是因为我是编程新手,而且我只知道 Java,所以我无法遵循我找到的最好的示例,因为它是用 C# 编写的。抱歉重复。 我将新的旋转数组命名为 Neo,因为我想要原始的和新的,但后来想起 new 不能用于命名变量,所以我使用 Neo,这意味着新的:)
最佳答案
这是对代码示例的简单修改。请注意旋转数组维度和旋转代数是如何更改的。
static int[][] rotateArrayCW(int[][] orig) {
final int rows = orig.length;
final int cols = orig[ 0 ].length;
final int[][] neo = new int[ cols ][ rows ];
for ( int r = 0; r < rows; r++ ) {
for ( int c = 0; c < cols; c++ ) {
neo[ c ][ rows - 1 - r ] = orig[ r ][ c ];
}
}
return neo;
}
关于java - 尝试将二维数组顺时针旋转 90 度,但它是逆时针旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45847027/