原题地址:https://leetcode.cn/problems/rotate-image/
找规律题目,写的不太python
'''
(0,0)->(0,3)
(1,0)->(0,2)
(2,0)->(0,1)
(3,0)->(0,0)
(0,1)->(1,3)
第i行j列->第j行n-i-1列
(i,j)->(j,n-i-1)->(n-i-1,n-j-1)->(n-j-1,i)->(i,j)
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
如果n%2 == 0, 则需要遍历左上角n/2为边长的正方形
如果n%2 == 1, 则需要遍历左上角n/2,n/2+1为边长的长方形
'''
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
temp = 0
n = len(matrix)
for i in range(n // 2):
for j in range((n + 1) // 2):
temp = matrix[i][j]
matrix[i][j] = matrix[n-j-1][i]
matrix[n-j-1][i] = matrix[n-i-1][n-j-1]
matrix[n-i-1][n-j-1] = matrix[j][n-i-1]
matrix[j][n-i-1] = temp