๐ Rotate Image - Transform Your Matrix Like a Pro!
Imagine you're working with digital image processing, and you need to rotate a square image by 90 degrees clockwise. You're given an n ร n 2D matrix representing an image, where each cell contains a pixel value.
Your challenge? Rotate the entire image in-place - meaning you must modify the original matrix directly without using any additional 2D matrices. This constraint makes the problem particularly interesting as you need to be clever about how you move elements around!
Example: If you have a 3ร3 matrix representing an image, after rotation:
[1,2,3] [7,4,1] [4,5,6] -> [8,5,2] [7,8,9] [9,6,3]
Notice how the first row becomes the last column, the second row becomes the middle column, and so on. Your task is to implement this transformation efficiently and elegantly!
Input & Output
Visualization
Time & Space Complexity
We still need to process each of the nยฒ elements exactly once
Only use a constant amount of extra space for temporary variables
Constraints
- n == matrix.length == matrix[i].length
- 1 โค n โค 20
- -1000 โค matrix[i][j] โค 1000
- Must be solved in-place with O(1) extra space