How do you rotate a two dimensional array?

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

    short normal[4][4] = {{8,4,7,5},{3,4,5,7},{9,5,5,6},{3,3,3,3}};
    
    short rotated[4][4];
    
    for (int r = 0; r < 4; ++r)
    {
      for (int c = 0; c < 4; ++c)
      {
        rotated[r][c] = normal[c][3-r];
      }
    }
    

    Simple C++ method, tho there would be a big memory overhead in a big array.

提交回复
热议问题