Practice/Meta/Leetcode 48. Rotate Image
CodingOptional
You are given a square 2D matrix (n × n) representing an image. Your task is to rotate the matrix by 90 degrees in the clockwise direction. The rotation must be performed in-place, meaning you cannot allocate another 2D matrix to solve this problem. You must modify the input matrix directly.
When rotating clockwise by 90 degrees:
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Explanation: Original: After 90° rotation: 1 2 3 7 4 1 4 5 6 → 8 5 2 7 8 9 9 6 3
Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] Explanation: Each element moves to its rotated position
Example 3:
Input: matrix = [[1]] Output: [[1]] Explanation: Single element remains in the same position