Skip to main content

48 - Rotate Image

Details

KeyValue
Linkhttps://leetcode.com/problems/rotate-image/
LanguagePython 3
Runtime58 ms, faster than 44.09% of Python3 online submissions for Rotate Image
Memory Usage13.9 MB, less than 29.99% of Python3 online submissions for Rotate Image
DatastructuresList[List[int]]
AlgorithmsFlip-Flip?
ComplexityTime: O(M) Memory: O(1)

Procedure

  1. ...

Code

Option 1

class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
matrix.reverse()
for i in range(len(matrix)):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

Option 2: One-Liner

class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
matrix[::] = zip(*matrix[::-1])

Option 3

class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
length = len(matrix)
for i in range(length):
for j in range(i+1,length):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

for row in matrix: row.reverse()