数组元素的重新排序 [英] Reordering of array elements

查看:123
本文介绍了数组元素的重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于数组

  [A1 A2 A3 ...的B1,B2,B3,...... BN C1 C2 C3 ... CN]
 

而无需使用额外的内存,你如何重新排序到一个数组

  [A1 B1 C1 A2 B2 C2 A3 B3 C3 ...一亿CN]
 

解决方案

你提的问题也被改述为如何做一个就地矩阵转置?。要知道为什么,想象你的两个阵列的每个子序列后添加一个换行符。这将会把第一阵列成的N×M矩阵,和第二阵列成的MxN矩阵

然而,这是不平凡的,非正方形矩阵。请参阅上就地矩阵转置的维基百科页面问题的COM prehensive说明其解决方案。

Given an array

[a1 a2 a3 ... an b1 b2 b3 ... bn c1 c2 c3 ...cn]

without using extra memory how do you reorder into an array

[a1 b1 c1 a2 b2 c2 a3 b3 c3 ... an bn cn]

解决方案

Your question can also be rephrased as 'How to do an in-place matrix transposition?'. To see why, imagine adding a newline after each subsequence in both of your arrays. This will turn the first array into an NxM matrix, and the second array into an MxN matrix.

Still, it is not trivial for non-square matrices. Please refer to the Wikipedia page on In-place matrix transposition for a comprehensive description of the problem and its solutions.

这篇关于数组元素的重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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