Rotate Image
You are given an n x n
2D matrix representing an image, rotate the image by 90 degrees (clockwise).
Function Signature:
from typing import List
def rotate(matrix: List[List[int]]) -> None:
"""
Rotate the given matrix by 90 degrees in-place.
Parameters:
- matrix: 2D List representing the image.
"""
# Your code here
Example:
# Input:
# matrix = [
# [1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]
# ]
#
# Output:
# [
# [7, 4, 1],
# [8, 5, 2],
# [9, 6, 3]
# ]
# Input:
# matrix = [
# [1]
# ]
#
# Output:
# [
# [1]
# ]
Note:
- You have to rotate the image in-place, which means you need to modify the input 2D matrix directly.
- If possible, do not use any extra space.
Instructions:
- Write the
rotate
function to solve the problem. - Implement your solution in Python.
- Provide clear comments in your code.
- Discuss the time and space complexity of your solution.
As always, we’ll share our solution to this problem tomorrow. Stay tuned 😊