Rotate Image LeetCode Solution
Rotate Image LeetCode Solution
- 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.
Example 1:
Input:matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]

Rotate Image LeetCode Solution:
- You are given an
n x n
2Dmatrix
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.
Constraints :
n == matrix.length == matrix[i].length
1 <= n <= 20
-1000 <= matrix[i][j] <= 1000

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]]
Rotate Image LeetCode Solution:
- First Column of Output (from Last Row of Input): The first column of the output matrix is created by taking the elements from the last row of the input matrix and placing them in the first column of the output matrix in reverse order. Input row: [15, 14, 12, 16] Output column: [15, 14, 12, 16]
- Second Column of Output (from Third Row of Input): The second column of the output matrix is formed by taking the elements from the third row of the input matrix and placing them in the second column of the output matrix in reverse order. Input row: [13, 3, 6, 7] Output column: [13, 3, 6, 7]
- Third Column of Output (from Second Row of Input): The third column of the output matrix is generated by taking the elements from the second row of the input matrix and placing them in the third column of the output matrix in reverse order. Input row: [2, 4, 8, 10] Output column: [10, 8, 4, 2]
- Fourth Column of Output (from First Row of Input): Finally, the fourth column of the output matrix is constructed by taking the elements from the first row of the input matrix and placing them in the fourth column of the output matrix in reverse order. Input row: [5, 1, 9, 11] Output column: [11, 9, 1, 5]
Output
[[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]
Complexity :
Time complexity:
O(n2n^2n
2
)
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Remove Duplicates from Sorted Array LeetCode Solution:
C++
Java
Python
C++
class Solution { public: void rotate(vector>& matrix) { vector > rows; for(int i = 0; i < matrix.size(); i++) { vector row; int count = 0; for(int j = matrix.size() - 1; j >= 0; j--) { row.push_back(matrix[j][i]); count += 1; } rows.push_back(row); } for(int i = 0; i < matrix.size(); i++) { for(int j = 0; j < matrix.size(); j++) { matrix[i][j] = rows[i][j]; } } } };
Java
class Solution { public void rotate(int[][] matrix) { int[][] rows = new int[matrix.length][matrix.length]; for(int i = 0; i < matrix.length; i++) { int[] row = new int[matrix.length]; int count = 0; for(int j = matrix.length - 1; j >= 0; j--) { row[count] = matrix[j][i]; count += 1; } rows[i] = row; } for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix.length; j++) { matrix[i][j] = rows[i][j]; } } } }
Python
class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ rows = [] for i in range(0, len(matrix)): row = [] for j in range(len(matrix) - 1, -1, -1): row += [matrix[j][i]] rows += [row] for i in range(0, len(matrix)): for j in range(0, len(matrix)): matrix[i][j] = rows[i][j]
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment