Possible Duplicate:
Algorithm to rotate an image 90 degrees in place? (No extra memory)
By saying 90 degrees i mean to say if:
A = {1,2,3, 4,5,6, 7,8,9}
then after 90 degree rotation A becomes:
A = {7,4,1, 8,5,2, 9,6,3}
Approach to solve this problem Take Input of a square matrix. Find the transpose of the matrix. Swap the element at index 0 with index n-1. Return the output.
90 Degree Rotation When rotating a point 90 degrees counterclockwise about the origin our point A(x,y) becomes A'(-y,x). In other words, switch x and y and make y negative.
Transpose and interchange rows or columns (depends whether you want to rotate left or right).
e. g.
1) original matrix 1 2 3 4 5 6 7 8 9 2) transpose 1 4 7 2 5 8 3 6 9 3-a) change rows to rotate left 3 6 9 2 5 8 1 4 7 3-b) or change columns to rotate right 7 4 1 8 5 2 9 6 3
All these operations can be done without allocating memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With