Rotate by 90 degree

Medium Accuracy: 53.41% Submissions: 12494 Points: 4


Given a square matrix[][] of size N x N. The task is to rotate it by 90 degrees in an anti-clockwise direction without using any extra space.

Example 1:

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

Your Task:
You only need to implement the given function rotate(). Do not read input, instead use the arguments given in the function. 




def rotate(matrix): 

    n = len(matrix);

    for i in range(0, int(n/2)):

    for j in range(i, n-i-1):

            temp = matrix[i][j];

            matrix[i][j] = matrix[j][n-1-i];        

            matrix[j][n-1-i] = matrix[n-1-i][n-1-j];

            matrix[n-1-i][n-1-j] = matrix[n-1-j][i];

            matrix[n-1-j][i] = temp;   

Comments

Popular posts from this blog

Large Factorial of array

Value equal to index value

Largest Element in Array