顺时针旋转数组
我有一个二维数组,需要顺时针旋转 90 度,但是我不断收到 arrayindexoutofbounds...
public int[][] rotateArray(int[][] arr) {
// first change the dimensions vertical length
// for horizontal length and vice versa
int[][] newArray = new int[arr[0].length][arr.length];
// invert values 90 degrees clockwise by starting
// from button of array to top and from left to right
int ii = 0;
int jj = 0;
for (int i = 0; i < arr[0].length; i++) {
for (int j = arr.length - 1; j >= 0; j--) {
newArray[ii][jj] = arr[i][j];
jj++;
}
ii++;
}
return newArray;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是一个标准矩阵顺时针旋转代码:
请注意以下几点:
M
和N
可以提高可读性mat
是有效的 MxN 矩阵,M>0, N>0
这是一个测试工具:
请注意 for-each 循环和 printMatrix 中的“noreferrer”>
java.util.Arrays
。如果您经常在 Java 中使用数组,那么您绝对应该熟悉它们。Java 矩阵库的链接
如果您经常使用矩阵,您可能需要考虑使用专门的矩阵库。
相关问题
从技术上讲,Java 有数组的数组。确保您了解所有含义。
Arrays.equals()
返回 < code>false 对于二维数组。Here's a standard matrix clockwise rotation code:
Note a few things:
M
andN
r, c
instead ofi, j
to index row and column of a matrixmat
is a valid MxN matrix,M>0, N>0
Here's a test harness:
Note the use of the for-each loop and
java.util.Arrays
inprintMatrix
. You should definitely familiarize yourself with them if you're working with arrays a lot in Java.Links to Java matrix libraries
If you're working with matrices a lot, you may want to consider using a specialized matrix library instead.
Related questions
Technically, Java has array of arrays. Make sure you understand all the implications.
Arrays.equals()
returnsfalse
for two dimensional arrays.我不明白你的循环的逻辑 - 不应该是
每个索引是否上升(如这里的 i )或下降(如这里的 j )的 Net(以及是否需要在分配中“翻转”其中一个或两个,例如在
的一侧使用
在作业中;-),因为arr.length-1-j
代替普通的j
>=arr
维度是arr.length
byarr[0].length
,并且对于newArray
反之亦然,在我看来,arr
上的第一个索引(newArray
上的第二个索引)必须是跨越 0 范围的索引包含到arr.length-1
,其他范围用于其他索引。这是一种“基本维度分析”(除了“维度”的含义与通常的“维度分析”不同,“维度分析”指的是物理维度,即时间、质量、长度,&c;-) 。 “翻转”和让每个循环上升或下降的问题取决于准确地可视化你的意思,我不是最伟大的“心理可视化者”,所以我认为,在现实生活中,我会尝试这个“的各种变体”轴换位”,直到我达到那个意思;-)。
I don't understand your loops' logic -- shouldn't it be
Net of whether each index goes up, like
i
here, or down, likej
here (and of whether either or both need to be "flipped" in the assignment, e.g usingarr.length-1-j
in lieu of plainj
on one side of the=
in the assignment;-), sincearr
dimensions arearr.length
byarr[0].length
, and vice versa fornewArray
, it seems to me that the first index onarr
(second onnewArray
) must be the one spanning the range from 0 toarr.length-1
included, and the other range for the other index.This is a kind of "basic dimensional analysis" (except that "dimension" is used in a different sense than normally goes with "dimensional analysis" which refers to physical dimensions, i.e., time, mass, length, &c;-). The issue of "flipping" and having each loop go up or down depend on visualizing exactly what you mean and I'm not the greatest "mental visualizer" so I think, in real life, I'd try the various variants of this "axis transposition" until I hit the one that's meant;-).
jj++
运行了i*j
次,这根本就不好。尝试在外循环中重置
jj
。jj++
is runi*j
times, and that can't be good at all.Try to reset
jj
in the outer loop.通用对象的解决方案:
用法:
Solution for generic objects:
usage:
我完全理解这个问题与 Swift 无关,但这里有一些详细的 Swift 4:
这个线程或当我在 Swift 中搜索问题时弹出的任何内容。
I completely understand that this question has nothing to do with Swift, but here's some verbose Swift 4:
This thread or whatever popped up when I searched for the question in Swift.
顺时针或逆时针旋转矩阵的步骤:
(或)
水平交换列(如果您想要逆时针旋转)
顺时针旋转程序:
逆时针旋转程序
n
= 行数或列数,我们可以在其中更改
n
,以上仅适用于经过测试且运行良好的方阵
Steps to Rotate a matrix clockwise or Anti-Clockwise:
(OR)
Swap Columns Horizontal (if You want Anti-Clockwise Rotation)
Program For Clockwise Rotation:
Program For Anti-Clockwise Rotation
n
= Number of rows or Number of ColumnsWhere We can change the
n
, The above are worked only for square MatrixTested and Worked Well
这是我经过验证的解决方案:
Here my verified solution: