posted 3 weeks ago
I was doing some exercise where I frequently needed to rotate or flip a 2D rectangular array (matrix).
Now, one way to do such thing is to have two nested loops, and swap the corresponding elements. But that means what I call 'a lot of index juggling'.
Suppose we have the methods 'int[] getColumn(int[][] array, int column)' and 'int[] reverse(int[] array)'.
When we rotate a matrix right, then we see that column 0 becomes row 0, albeit reversed. So our method can become:
Can you find similar methods for rotating left and flipping horizontally or vertically?
There are three kinds of actuaries: those who can count, and those who can't.