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.
Leetcode

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.

rotate image leetcode solution

Rotate Image LeetCode Solution:

  1. 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]
  2. 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]
  3. 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]
  4. 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]]

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: 

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription