MEDIUM NC#136 Blind #48 Math & Geometry
48. Rotate Image
š Problem
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
š§ Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- ā¢Array methods (`push`, `pop`, `shift`, `slice`)
- ā¢Object/Map/Set usage patterns
- ā¢Function parameter and return typing
Logical Thinking Concepts
- ā¢Define invariants before coding
- ā¢Check edge cases first (`[]`, single element, duplicates)
- ā¢Estimate time/space before implementation
š” Approach
- ā Transpose the matrix (swap rows and columns)
- ā Reverse each row
- ā Combined effect is 90-degree clockwise rotation
- ā Time: O(n²), Space: O(1)
š§ Prerequisites
š ļø Hints & Pitfalls
Hints
- ā¢Transpose the matrix (swap rows and columns)
- ā¢Reverse each row
- ā¢Combined effect is 90-degree clockwise rotation
Common Pitfalls
- ā¢Time: O(n²), Space: O(1)
š§Ŗ Test Cases
Test Case 1
Not run Input:
rotate(matrix1); Expected:
null Test Case 2
Not run Input:
rotate(matrix2); Expected:
null Test Case 3
Not run Input:
rotate(matrix: number[][]); Expected:
Computed from hidden reference š Code Editor
š¤ Output