Sitemap

Rotate Image, JavaScript, LeetCode

2 min readOct 28, 2021

--

In this article I will be going over the Rotate Image problem from the Array section of LeetCode’s Top Interview Questions (Easy Collection).

Press enter or click to view image in full size

Here is the question as on LeetCode:

Press enter or click to view image in full size
Press enter or click to view image in full size

This question looks more complicated than it actually is. I found it easier just to look at the arrays in the input and output matrix to determine how to approach the problem. The output is arranged so that each sub-array is comprised of values in descending order of the input outer array and ascending order within the input sub-arrays. So the first output sub-array has the first value of each of the input sub-arrays, starting with the last, descending to the first.

Get K. Johnson’s stories in your inbox

Join Medium for free to get updates from this writer.

I immediately recognized that I would need to do a nested loop to keep track of the indexes of both outer and inner arrays of the matrix. I started with a nested for loop (within a for loop) then realized a for loop within a while loop would be more appropriate.

Here is my solution:

I told you it was simple. Although LeetCode did not accept this submission, it worked for all the examples in my console, and you can even see the indexes and arrays logged. I’m not sure if LeetCode is bugging or if I’m missing something… let me know if you’ve figured it out!

Here is a link to a passing solution to this problem: https://code.likeagirl.io/rotate-an-2d-matrix-90-degree-clockwise-without-create-another-array-49209ea8b6e6

--

--