Rotate a Matrix by 90 Degrees
- Shreyas Naphad
- Jun 4, 2024
- 1 min read
Updated: Jun 6, 2024
In this article, we aim to solve the problem where we have to rotate a matrix given to us by 90 degrees. This simply means that we have to just rotate the given matrix in a 90 degree clockwise direction. Let us understand this through an example:
Example:
Input: [[10,20,30],[44,55,66],[77,88,99]]
Output: [[77,44,10],[88,55,20],[99,66,30]]
Solution:
Explanation:
· To rotate a matrix by 90 degrees, we have initially taken the transpose of the given matrix.
· Finally we reverse every row of the transposed matrix and this is how we get a matrix rotated by 90 degrees.
Time Complexity: O(N*N)
Space Complexity: O(1)
Comments