Rotate Image - Leetcode Solution

 # Intuition

First, we will find the transpose matrix and then swap the first and last columns.

# Complexity
- Time complexity: O(N^2)


- Space complexity: O(1)


# Code
```
class Solution {
// TC=O(N^2)
// SC=O(1)
public:
    void rotate(vector<vector<int>>& matrix) {
        int n=matrix.size();
        //This for loop is used to Transpose the matrix
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
                swap(matrix[i][j],matrix[j][i]);
            }
        }
        //This for loop will swap the first column and last column
        for(int i=0;i<n;i++)
        {
            reverse(matrix[i].begin(),matrix[i].end());
        }
    }
};
```

If you like the solution, please comment and share it with your friends.

Keep Learning, Keep Growing.

Comments

Popular posts from this blog

Solution of Image Id is not recognised by Glide library in android studio

Linked List Cycle II - Leetcode Solution

Leetcode Solution - Remove Duplicates from Sorted List II