旋转二维矩形阵列的就地 [英] Rotate 2D rectangular array in-place

查看:100
本文介绍了旋转二维矩形阵列的就地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非方形阵列是这样的:

I have an non-square array like this:

const int dim1 = 3, dim2 = 4;
int array[12] = { 1, 2, 3, 
                  4, 5, 6,
                  7, 8, 9,
                 10,11,12};

和我需要将其转换为:

{3,6,9,12,
 2,5,8,11,
 1,4,7,10}

这就是旋转/洗牌它逆时针(或顺时针,算法应该是similiar)。

that is, rotate/shuffle it counter-clockwise (or clockwise, the algorithm should be similiar).

该算法应使用的最小空间。我必须在一个非常内存受限的环境下旋转图像,所以更少的空间就更好了。速度不是大问题。

The algorithm should use a minimal amount of space. I have to rotate an image in an extremely memory-constrained environment, so the less space the better. Speed is not as big an issue.

推荐答案

您可以转就地矩阵(见<一href=\"http://en.wikipedia.org/wiki/In-place_matrix_transposition\">http://en.wikipedia.org/wiki/In-place_matrix_transposition)再反向行是微不足道的。

You can transpose the matrix in-place (see http://en.wikipedia.org/wiki/In-place_matrix_transposition) and then reverse the rows which is trivial.

这篇关于旋转二维矩形阵列的就地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆