Rotate Image
You are given an n x n
2D matrix
representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
class Solution:
def rotate(self, m: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
l, r = 0, len(m)-1
while l < r :
for i in range(r-l):
t, b = l, r
# tl in temp
temp = m[t][l+i]
# bl -> tl
m[t][l+i] = m[b-i][l]
# br -> bl
m[b-i][l] = m[b][r-i]
# tr -> br
m[b][r-i] = m[t+i][r]
# temp -> tr
m[t+i][r] = temp
r -= 1
l += 1
Explaintion :
Comments
Post a Comment