How do you rotate a two dimensional array?

后端 未结 30 3082
耶瑟儿~
耶瑟儿~ 2020-11-22 02:43

Inspired by Raymond Chen\'s post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I\'d

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 03:20

    A couple of people have already put up examples which involve making a new array.

    A few other things to consider:

    (a) Instead of actually moving the data, simply traverse the "rotated" array differently.

    (b) Doing the rotation in-place can be a little trickier. You'll need a bit of scratch place (probably roughly equal to one row or column in size). There's an ancient ACM paper about doing in-place transposes (http://doi.acm.org/10.1145/355719.355729), but their example code is nasty goto-laden FORTRAN.

    Addendum:

    http://doi.acm.org/10.1145/355611.355612 is another, supposedly superior, in-place transpose algorithm.

提交回复
热议问题