How do you rotate a two dimensional array?

后端 未结 30 3591
耶瑟儿~
耶瑟儿~ 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:43

    There are tons of good code here but I just want to show what's going on geometrically so you can understand the code logic a little better. Here is how I would approach this.

    first of all, do not confuse this with transposition which is very easy..

    the basica idea is to treat it as layers and we rotate one layer at a time..

    say we have a 4x4

    1   2   3   4
    5   6   7   8
    9   10  11  12
    13  14  15  16
    

    after we rotate it clockwise by 90 we get

    13  9   5   1
    14  10  6   2   
    15  11  7   3
    16  12  8   4
    

    so let's decompose this, first we rotate the 4 corners essentially

    1           4
    
    
    13          16
    

    then we rotate the following diamond which is sort of askew

        2
                8
    9       
            15
    

    and then the 2nd skewed diamond

            3
    5           
                12
        14
    

    so that takes care of the outer edge so essentially we do that one shell at a time until

    finally the middle square (or if it's odd just the final element which does not move)

    6   7
    10  11
    

    so now let's figure out the indices of each layer, assume we always work with the outermost layer, we are doing

    [0,0] -> [0,n-1], [0,n-1] -> [n-1,n-1], [n-1,n-1] -> [n-1,0], and [n-1,0] -> [0,0]
    [0,1] -> [1,n-1], [1,n-2] -> [n-1,n-2], [n-1,n-2] -> [n-2,0], and [n-2,0] -> [0,1]
    [0,2] -> [2,n-2], [2,n-2] -> [n-1,n-3], [n-1,n-3] -> [n-3,0], and [n-3,0] -> [0,2]
    

    so on and so on until we are halfway through the edge

    so in general the pattern is

    [0,i] -> [i,n-i], [i,n-i] -> [n-1,n-(i+1)], [n-1,n-(i+1)] -> [n-(i+1),0], and [n-(i+1),0] to [0,i]
    

提交回复
热议问题