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. 
Expected Time Complexity: O(N*N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 50
1 <= matrix[][] <= 100

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: Matrices

Data structures

Starter code: #User function Template for python3

def rotate(matrix): 
    #code here

Solutions:
def rotate(matrix):
	l = []
	for i in range(-1, -(len(matrix) + 1), -1):
		k = []
		for j in range(len(matrix)):
			k.append(matrix[j][i])
		l.append(k)
	for i in range(len(matrix)):
		matrix[i] = l[i]


def rotate(matrix):
	n = len(matrix)
	rotated = []

	def transpose(value):
		temp = []
		for v in matrix:
			i = n - value
			temp.append(v[i])
		rotated.append(temp)
	for i in range(n):
		transpose(i + 1)
	for (i, item) in enumerate(rotated):
		for (j, v) in enumerate(item):
			matrix[i][j] = v


def rotate(matrix):
	li = []
	for i in zip(*matrix):
		li.append(list(i))
	li = li[::-1]
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			matrix[i][j] = li[i][j]


def rotate(matrix):
	n = len(matrix)
	for i in range(0, n // 2):
		for j in range(0, n // 2):
			(matrix[i][j], matrix[n - j - 1][i], matrix[n - i - 1][n - j - 1], matrix[j][n - i - 1]) = (matrix[j][n - i - 1], matrix[i][j], matrix[n - j - 1][i], matrix[n - i - 1][n - j - 1])
	if n % 2:
		for r in range(0, n // 2):
			(i, j) = (r, n // 2)
			(matrix[i][j], matrix[n - j - 1][i], matrix[n - i - 1][n - j - 1], matrix[j][n - i - 1]) = (matrix[j][n - i - 1], matrix[i][j], matrix[n - j - 1][i], matrix[n - i - 1][n - j - 1])


def rotate(A):

	def rotate2(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[j][N - 1 - i]
				A[j][N - 1 - i] = A[N - 1 - i][N - 1 - j]
				A[N - 1 - i][N - 1 - j] = A[N - 1 - j][i]
				A[N - 1 - j][i] = temp
	rotate2(A)


def rotate(matrix):
	l = []
	for i in zip(*matrix):
		l = [i] + l
	matrix[:] = l[:]


def rotate(matrix):
	left = 0
	right = len(matrix) - 1
	while left <= right:
		for i in range(right - left):
			top = left
			bottom = right
			temp = matrix[top][left + i]
			matrix[top][left + i] = matrix[top + i][right]
			matrix[top + i][right] = matrix[bottom][right - i]
			matrix[bottom][right - i] = matrix[bottom - i][left]
			matrix[bottom - i][left] = temp
		left += 1
		right -= 1
	return matrix


def rotate(matrix):
	for i in range(N):
		a = 0
		b = N - 1
		while a <= b:
			(matrix[i][a], matrix[i][b]) = (matrix[i][b], matrix[i][a])
			a += 1
			b -= 1
	for i in range(N):
		for j in range(i):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	return matrix


def rotate(matrix):
	n = len(matrix)
	for i in range(n):
		for j in range(i + 1, n):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	matrix.reverse()
	return matrix


def rotate(arr):
	for lst in arr:
		i = 0
		j = len(lst) - 1
		while i < j:
			(lst[i], lst[j]) = (lst[j], lst[i])
			i += 1
			j -= 1
	for i in range(0, len(arr)):
		for j in range(0, len(arr)):
			if i < j:
				(arr[i][j], arr[j][i]) = (arr[j][i], arr[i][j])
	return arr


def rotate(matrix):
	for i in range(len(matrix)):
		for j in range(i, len(matrix)):
			temp = matrix[i][j]
			matrix[i][j] = matrix[j][i]
			matrix[j][i] = temp
	for i in range(len(matrix)):
		j = 0
		k = len(matrix) - 1
		while j < k:
			(matrix[k][i], matrix[j][i]) = (matrix[j][i], matrix[k][i])
			j = j + 1
			k = k - 1
	return matrix


def rotate(matrix):
	row = len(matrix)
	col = len(matrix[0])
	for i in range(row):
		for j in range(col // 2):
			(matrix[i][j], matrix[i][col - j - 1]) = (matrix[i][col - j - 1], matrix[i][j])
	for i in range(row):
		for j in range(i, col):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	return matrix


def rotate(matrix):
	for i in range(len(matrix)):
		(s, e) = (0, len(matrix) - 1)
		while s < e:
			(matrix[i][s], matrix[i][e]) = (matrix[i][e], matrix[i][s])
			s += 1
			e -= 1
	for i in range(len(matrix)):
		for j in range(i):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])


def rotate(matrix):
	copy = matrix[:]
	matrix.clear()
	length = len(copy)
	Index = -1
	for i in range(length - 1, -1, -1):
		Index += 1
		matrix.append([])
		for j in range(length):
			matrix[Index].append(copy[j][i])
	return matrix


def rotate(matrix):
	n = len(matrix)
	for i in range(n):
		for j in range(i, n):
			t = matrix[i][j]
			matrix[i][j] = matrix[j][i]
			matrix[j][i] = t
	for i in range(n // 2):
		for j in range(n):
			t = matrix[i][j]
			matrix[i][j] = matrix[n - 1 - i][j]
			matrix[n - 1 - i][j] = t


def rotate(matrix):
	N = len(matrix)
	for i in range(N):
		for j in range(i + 1, N):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	for i in range(N):
		low = 0
		high = N - 1
		while low < high:
			(matrix[low][i], matrix[high][i]) = (matrix[high][i], matrix[low][i])
			low += 1
			high -= 1
	return matrix


def rotate(matrix):
	n = len(matrix)
	for i in range(n):
		matrix[i] = matrix[i][::-1]
	for i in range(n):
		for j in range(i, n):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])


def rotate(matrix):
	n = len(matrix)
	for i in range(0, n // 2):
		for j in range(i, n - 1 - i):
			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
	return matrix


def rotate(matrix):
	l = len(matrix)
	for i in range(l):
		matrix[i] = matrix[i][::-1]
	for i in range(l):
		for j in range(i, l):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	return matrix


def rotate(matrix):
	m = len(matrix)
	n = len(matrix[0])
	for row in matrix:
		row = row.reverse()
	for i in range(m):
		for j in range(i):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	return matrix


def rotate(matrix):
	for i in range(len(matrix)):
		matrix[i].reverse()
	for i in range(len(matrix)):
		for j in range(i):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])


def rotate(mat):
	N = len(matrix[0])
	for x in range(0, int(N / 2)):
		for y in range(x, N - x - 1):
			temp = mat[x][y]
			mat[x][y] = mat[y][N - 1 - x]
			mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y]
			mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x]
			mat[N - 1 - y][x] = temp


def rotate(matrix):
	n = len(matrix)
	x = []
	for i in range(n):
		v = []
		for j in range(n):
			v.append(matrix[j][i])
		x.append(v)
	x.reverse()
	for i in range(n):
		for j in range(n):
			matrix[j][i] = x[j][i]
	return matrix


def rotate(matrix):
	for i in range(N // 2):
		for j in range(i, N - i - 1):
			temp = matrix[i][j]
			matrix[i][j] = matrix[j][N - i - 1]
			matrix[j][N - i - 1] = 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


def rotate(matrix):
	n = len(matrix)
	list = []
	for i in range(len(matrix)):
		row = []
		for j in range(len(matrix)):
			row.append(matrix[j][i])
		list.append(row)
	for k in range(n // 2):
		for l in range(n):
			(list[k][l], list[n - 1 - k][l]) = (list[n - 1 - k][l], list[k][l])
	for i in range(len(list)):
		matrix[i] = list[i]
	return matrix


def rotate(matrix):
	mat = matrix
	for i in range(len(mat)):
		mat[i].reverse()
	for i in range(len(mat)):
		for j in range(i, len(mat)):
			(mat[i][j], mat[j][i]) = (mat[j][i], mat[i][j])
	return matrix


import numpy as np

def rotate(matrix):
	k = np.transpose(matrix)
	k = k[::-1]
	for i in range(len(k)):
		for j in range(len(k)):
			matrix[i][j] = k[i][j]


def rotate(mat):
	n = len(mat)
	for i in range(n // 2):
		for j in range(i, n - i - 1):
			temp = mat[i][j]
			mat[i][j] = mat[j][n - i - 1]
			mat[j][n - i - 1] = mat[n - i - 1][n - j - 1]
			mat[n - i - 1][n - j - 1] = mat[n - j - 1][i]
			mat[n - j - 1][i] = temp
	return mat


def rotate(matrix):
	for i in range(len(matrix)):
		for j in range(i):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	for i in range(len(matrix) // 2):
		for j in range(len(matrix)):
			(matrix[i][j], matrix[len(matrix) - i - 1][j]) = (matrix[len(matrix) - i - 1][j], matrix[i][j])
	return matrix


def rotate(matrix):
	n = len(matrix)
	for i in range(n):
		for j in range(i, n):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	(t, b) = (0, n - 1)
	while t < b:
		(matrix[t], matrix[b]) = (matrix[b], matrix[t])
		t += 1
		b -= 1


def rotate(m):
	n = len(m)
	for i in range(n):
		m[i] = m[i][::-1]
	for i in range(n):
		for j in range(i, n):
			(m[i][j], m[j][i]) = (m[j][i], m[i][j])


def rotate(matrix):
	for i in range(N):
		for j in range(i + 1, N):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	return matrix.reverse()


def rotate(matrix):
	a = list(zip(*matrix))
	a.reverse()
	for i in range(len(matrix)):
		matrix[i] = list(a[i])


def rotate(matrix):
	x = len(matrix)
	for i in range(x):
		for j in range(i, x):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	start = 0
	end = x - 1
	while start < end:
		for i in range(x):
			(matrix[start][i], matrix[end][i]) = (matrix[end][i], matrix[start][i])
		start += 1
		end -= 1


def rotate(matrix):
	m = len(matrix)
	n = len(matrix[0])
	for i in range(m):
		for j in range(i):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	for i in range(m // 2):
		for j in range(n):
			(matrix[n - 1 - i][j], matrix[i][j]) = (matrix[i][j], matrix[n - 1 - i][j])


def rotate(matrix):
	(left, right) = (0, len(matrix) - 1)
	while left < right:
		for i in range(right - left):
			(top, bottom) = (left, right)
			topRight = matrix[top][right - i]
			matrix[top][right - i] = matrix[bottom - i][right]
			matrix[bottom - i][right] = matrix[bottom][left + i]
			matrix[bottom][left + i] = matrix[top + i][left]
			matrix[top + i][left] = topRight
		left += 1
		right -= 1


import math

def rotate(matrix):
	(row, col) = (len(matrix), len(matrix))
	for i in range(row):
		for j in range(col // 2):
			(matrix[i][j], matrix[i][col - 1 - j]) = (matrix[i][col - 1 - j], matrix[i][j])
	for k in range(row):
		for l in range(k, col, 1):
			(matrix[k][l], matrix[l][k]) = (matrix[l][k], matrix[k][l])


def rotate(matrix):
	for i in range(len(matrix)):
		for j in range(len(matrix[0]) // 2):
			k = matrix[i][j]
			matrix[i][j] = matrix[i][len(matrix[0]) - 1 - j]
			matrix[i][len(matrix[0]) - 1 - j] = k
	for i in range(1, len(matrix)):
		for j in range(i):
			k = matrix[i][j]
			matrix[i][j] = matrix[j][i]
			matrix[j][i] = k


def rotate(matrix):
	for i in range(len(matrix)):
		matrix[i] = matrix[i][::-1]
	row = 0
	limit = 0
	while row < len(matrix):
		for cl in range(limit, len(matrix[0])):
			(matrix[row][cl], matrix[cl][row]) = (matrix[cl][row], matrix[row][cl])
		row += 1
		limit += 1
	return matrix


def rotate(matrix):
	n = len(matrix)
	for i in range(n):
		for j in range(i, n):
			temp = matrix[i][j]
			matrix[i][j] = matrix[j][i]
			matrix[j][i] = temp
	for i in range(n):
		top = 0
		bottom = n - 1
		while top <= bottom:
			temp = matrix[top][i]
			matrix[top][i] = matrix[bottom][i]
			matrix[bottom][i] = temp
			top += 1
			bottom -= 1
	return matrix


def rotate(matrix):
	l = []
	for i in zip(*matrix):
		l.append(i)
	matrix[:] = l[::-1]


def rotate(matrix):
	n = len(matrix)
	for i in range(n - 1):
		for j in range(0, n - 1 - i):
			(matrix[i][j], matrix[n - 1 - j][n - 1 - i]) = (matrix[n - 1 - j][n - 1 - i], matrix[i][j])
	for row in matrix:
		l = 0
		r = n - 1
		while l < r:
			(row[l], row[r]) = (row[r], row[l])
			r -= 1
			l += 1


def rotate(mat):
	r = len(mat)
	c = len(mat[0])
	for i in range(r):
		mat[i] = mat[i][::-1]
	for i in range(r):
		for j in range(i):
			(mat[i][j], mat[j][i]) = (mat[j][i], mat[i][j])
	return mat


import numpy as np

def rotate(matrix):
	size = len(matrix)
	np_matrix = np.array(matrix)
	reversed_imatrix = np.zeros((size, size), dtype=int)
	for i in range(size):
		reversed_imatrix[i, size - i - 1] = 1
	result = np.transpose(np.dot(np_matrix, reversed_imatrix))
	for i in range(size):
		for j in range(size):
			matrix[i][j] = result[i, j]


def rotate(matrix):
	n = len(matrix)
	i = 0
	j = 1
	while i < n - 1 and j < n:
		a = matrix[i][j]
		b = matrix[j][i]
		matrix[i][j] = b
		matrix[j][i] = a
		if j == n - 1:
			i += 1
			j = i + 1
		else:
			j += 1
	i = 0
	j = 0
	while i < n // 2 and j < n:
		a = matrix[n - 1 - i][j]
		b = matrix[i][j]
		matrix[i][j] = a
		matrix[n - 1 - i][j] = b
		if j == n - 1:
			j = 0
			i += 1
		else:
			j += 1
	return matrix


def rotate(matrix):
	l = []
	n = len(matrix)
	for i in range(n - 1, -1, -1):
		l1 = []
		for j in range(n):
			l1.append(matrix[j][i])
		l.append(l1)
	for i in range(n):
		for j in range(n):
			matrix[i][j] = l[i][j]
	return l


from itertools import product

def rotate(matrix):
	n = len(matrix)
	for row in matrix:
		row[:] = row[::-1]
	for i in range(n):
		for j in range(i, n):
			(matrix[j][i], matrix[i][j]) = (matrix[i][j], matrix[j][i])


def rotate(matrix):
	col = len(matrix[0])
	for i in range(col):
		for j in range(i):
			(matrix[j][i], matrix[i][j]) = (matrix[i][j], matrix[j][i])
	for i in range(col):
		st = 0
		end = col - 1
		while st < end:
			(matrix[st][i], matrix[end][i]) = (matrix[end][i], matrix[st][i])
			st += 1
			end -= 1
	return matrix


import math

def rotate(matrix):
	n = len(matrix)
	for i in matrix:
		i.reverse()
	for i in range(n):
		for j in range(i + 1, n):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])


import numpy as np

def rotate(matrix):
	ls = np.ravel(matrix)
	d = len(matrix) - 1
	x = 0
	arr = []
	for i in range(0, len(matrix)):
		x = d
		temp = []
		for j in range(0, len(matrix)):
			temp.append(ls[x])
			x = x + N
		d -= 1
		arr.append(temp)
	for i in range(0, len(matrix)):
		matrix[i][:] = arr[i][:]


def rotate(matrix):
	n = len(matrix)
	m = len(matrix[0])
	for i in range(n):
		for j in range(i, m):
			(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
	(li, ri) = (0, n - 1)
	while li < ri:
		(matrix[li], matrix[ri]) = (matrix[ri], matrix[li])
		li += 1
		ri -= 1
	return matrix


def rotate(arr):
	for i in range(len(arr)):
		for j in range(len(arr[0])):
			if i < j:
				(arr[i][j], arr[j][i]) = (arr[j][i], arr[i][j])
	arr.reverse()


def rotate(matrix):
	for i in range(len(matrix)):
		for j in range(i + 1, len(matrix[0])):
			(matrix[j][i], matrix[i][j]) = (matrix[i][j], matrix[j][i])
	matrix.reverse()


def rotate(matrix):
	for i in range(len(matrix)):
		matrix[i].reverse()
	for i in range(len(matrix)):
		for j in range(len(matrix[i])):
			if i != j:
				(matrix[i][j], matrix[j][i]) = (matrix[j][i], matrix[i][j])
			else:
				break
	return matrix


def rotate(arr):
	n = len(arr)
	for i in range(0, n):
		arr[i].reverse()
	for i in range(0, n):
		for j in range(i, n):
			temp = arr[i][j]
			arr[i][j] = arr[j][i]
			arr[j][i] = temp


def transpose(arr):
	row = len(arr)
	col = len(arr[0])
	for i in range(row):
		for j in range(i, col):
			temp = arr[i][j]
			arr[i][j] = arr[j][i]
			arr[j][i] = temp

def rotate(arr):
	row = len(arr)
	col = len(arr[0])
	transpose(arr)
	for i in range(col):
		start = 0
		end = row - 1
		while start < end:
			temp = arr[start][i]
			arr[start][i] = arr[end][i]
			arr[end][i] = temp
			start += 1
			end -= 1