Skip to main content

Rotate Image

Question

Given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

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

Output: [[7,4,1],
[8,5,2],
[9,6,3]]

Solution

all//Rotate Image.py


def rotateImage(a):
N = len(a[0])
for i in range(N // 2):
for j in range(i, N - i - 1):
temp = a[i][j]
a[i][j] = a[N - 1 - j][i]
a[N - 1 - j][i] = a[N - 1 - i][N - 1 - j]
a[N - 1 - i][N - 1 - j] = a[j][N - 1 - i]
a[j][N - 1 - i] = temp
return a