Problem statement:
Given a matrix, your task is to rotate the matrix by 90 degrees.
(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.)
Input Format:
Matrix representing an image of n*n dimension
Output Format:
Matrix representing the rotated image by 90 degree clockwise
Sample Input 1:
[[1,2,3],[4,5,6],[7,8,9]]
Sample Output 1:
[[7,4,1],[8,5,2],[9,6,3]]
Sample Input 2:
[[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Sample Output 2:
[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Approach:
Take another dummy matrix of n*n, and then take the first row of the matrix and put it in the last column of the dummy matrix, take the second row of the matrix, and put it in the second last column of the matrix and so.
Time Complexity: O(n²)
Space Complexity: O(n²)
Code:
Thanks for Reading
Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.
Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.