rotate_image
1# @leet start 2class Solution: 3 def rotate(self, matrix: list[list[int]]) -> None: 4 """ 5 This function rotates a matrix 90 degrees. 6 To do so, it grabs the four outermost cells and then rotates them like this: 7  8 """ 9 n = len(matrix[0]) 10 for i in range(n // 2 + n % 2): 11 for j in range(n % 2): 12 tmp = matrix[n - 1 - j][i] 13 matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1] 14 matrix[n - 1 - i][n - i - 1] = matrix[j][n - 1 - i] 15 matrix[j][n - 1 - i] = matrix[i][j] 16 matrix[i][j] = tmp 17 18 19# @leet end 20 21 22def test(): 23 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def rotate(self, matrix: list[list[int]]) -> None: 5 """ 6 This function rotates a matrix 90 degrees. 7 To do so, it grabs the four outermost cells and then rotates them like this: 8  9 """ 10 n = len(matrix[0]) 11 for i in range(n // 2 + n % 2): 12 for j in range(n % 2): 13 tmp = matrix[n - 1 - j][i] 14 matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1] 15 matrix[n - 1 - i][n - i - 1] = matrix[j][n - 1 - i] 16 matrix[j][n - 1 - i] = matrix[i][j] 17 matrix[i][j] = tmp
def
rotate(self, matrix: list[list[int]]) -> None:
4 def rotate(self, matrix: list[list[int]]) -> None: 5 """ 6 This function rotates a matrix 90 degrees. 7 To do so, it grabs the four outermost cells and then rotates them like this: 8  9 """ 10 n = len(matrix[0]) 11 for i in range(n // 2 + n % 2): 12 for j in range(n % 2): 13 tmp = matrix[n - 1 - j][i] 14 matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1] 15 matrix[n - 1 - i][n - i - 1] = matrix[j][n - 1 - i] 16 matrix[j][n - 1 - i] = matrix[i][j] 17 matrix[i][j] = tmp
This function rotates a matrix 90 degrees.
To do so, it grabs the four outermost cells and then rotates them like this:

def
test():