+)Do code). """ You are given an n x n 2D mat representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? """
+)Do code). """ You are given an n x n 2D mat representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? """
Chapter16: Graphics
Section: Chapter Questions
Problem 9RQ
Related questions
Question
+)Do code).
"""
You are given an n x n 2D mat representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
"""
# clockwise rotate
# first reverse up to down, then swap the symmetry
# 1 2 3 7 8 9 7 4 1
# 4 5 6 => 4 5 6 => 8 5 2
# 7 8 9 1 2 3 9 6 3
def rotate(mat):
if not mat:
return mat
mat.reverse()
for i in range(len(mat)):
for j in range(i):
mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
return mat
if __name__ == "__main__":
mat = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]].
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage