DEV Community

Cover image for Leetcode 48 : Rotate Image
Rohith V
Rohith V

Posted on

4

Leetcode 48 : Rotate Image

Problem Statement :

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.

Test Cases :

Example 1:

Sample 1

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:

Sample 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]]
Example 3:

Input: matrix = [[1]]
Output: [[1]]
Example 4:

Input: matrix = [[1,2],[3,4]]
Output: [[3,1],[4,2]]

Constraints:

  • matrix.length == n

  • matrix[i].length == n

  • 1 <= n <= 20

  • -1000 <= matrix[i][j] <= 1000

Algorithm :

The simple idea is, consider this matrix



matrix = [[1,2,3],[4,5,6],[7,8,9]]


Enter fullscreen mode Exit fullscreen mode


1 2 3
4 5 6
7 8 9

Just take the transpose and lets see what we got :

1 2 3           1 4 7
4 5 6.      =>  2 5 8   
7 8 9           3 6 9

So we got the above after doing the transpose operation.


Enter fullscreen mode Exit fullscreen mode


for (int i=0; i<row; i++) {
            for (int j=0; j<i; j++) {
                swap(matrix, i, j);
            }
        }


Enter fullscreen mode Exit fullscreen mode


Now let's compare it with our possible answer.
Our transpose is :

1 4 7                            7 4 1
2 5 8.     and our answer is     8 5 2
3 6 9                            9 6 3

So what we observe, answer is just reversing each row after doing the transpose.

1 4 7. after reversing 7 4 1
2 5 8  after reversing 8 5 2
3 6 9  after reversing 9 6 3

which is what we needed as our answer.


Enter fullscreen mode Exit fullscreen mode

So algorithm is pretty simple :

1) Take the transpose of the given matrix
2) Reverse each row



Note : The above is 90 degree clockwise direction. What if, we need to rotate 90 degree anti clockwise direction.

Same way, just take the transpose.

1 2 3           1 4 7
4 5 6.      =>  2 5 8   
7 8 9           3 6 9

Now what will be our possible answer if we do anti clockwise 90 degree rotation on given matrix.

1 2 3           3 6 9
4 5 6.      =>  2 5 8   
7 8 9           1 4 7

Now we can just observe the answer along with the transpose of the matrix

1 4 7                            3 6 9
2 5 8.     and our answer is     2 5 8
3 6 9                            1 4 7

So we can observe that if we just reverse all the column, we will get the desired output.

1 4 7. after reversing column wise 3 6 9
2 5 8                              2 5 8
3 6 9                              1 4 7


Enter fullscreen mode Exit fullscreen mode

Complexity :

Time complexity is O(row * col) and since we are doing in-place, space complexity is O(1).

Code : (Clockwise 90 degree rotation)



class Solution {
    public void rotate(int[][] matrix) {
        if (matrix == null || matrix.length == 0) {
            return;
        }
        int row = matrix.length;
        int col = matrix[0].length;
        for (int i=0; i<row; i++) {
            for (int j=0; j<i; j++) {
                swap(matrix, i, j);
            }
        }
        for (int [] rowWise : matrix) {
            reverse(rowWise, 0, col - 1);
        }
    }

    private void swap(int [][] matrix, int i, int j) {
        int temp = matrix[i][j];
        matrix[i][j] = matrix[j][i];
        matrix[j][i] = temp;
    }

    private void reverse(int [] nums, int i, int j) {
        while (i < j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            i++;
            j--;
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

Github :

GitHub logo Rohithv07 / LeetCode

LeetCode problems that are solved.

LeetCode

LeetCode problems that are solved.

  • take the bit of the ith(from right) digit:

    bit = (mask >> i) & 1;

  • set the ith digit to 1: mask = mask | (1 << i);

  • set the ith digit to 0: mask = mask & (~(1 << i));

A Sample Structure




GitHub logo Rohithv07 / LeetCodeTopInterviewQuestions

Leetcode Top Interview questions discussed in Leetcode. https://leetcode.com/explore/interview/card/top-interview-questions




Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post