Reordering of array elements

前端 未结 4 412
予麋鹿
予麋鹿 2020-12-02 19:24

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



        
4条回答
  •  半阙折子戏
    2020-12-02 20:22

    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.

提交回复
热议问题