Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- 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, matrix: List[List[int]]) -> None:
- """
- Do not return anything, modify matrix in-place instead.
- mat[r][c] -> mat[c][(n - 1) - r]
- => mat[r][c] <- mat[c][r] <- mat[c][(n-1)-r]
- """
- n = len(matrix)
- for r in range(len(matrix)):
- for c in range(len(matrix[0])):
- if r < c:
- continue
- matrix[r][c], matrix[c][r] = matrix[c][r], matrix[r][c]
- for c in range(len(matrix)):
- for r in range(len(matrix)):
- if (n - 1) - r > r:
- continue
- matrix[c][r], matrix[c][(n - 1) - r] = matrix[c][(n - 1) - r], matrix[c][r]
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement