力扣 48. 旋转图像

矩阵处理(转置 + 列调换)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def rotate(matrix):
"""
Do not return anything, modify matrix in-place instead.
"""
if not matrix or not matrix[0]:
return

height = len(matrix)

for i in range(height):
for j in range(i, height):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]


for i in range(height // 2):
for j in range(height):
matrix[j][i], matrix[j][height - 1 - i] = matrix[j][height - 1 - i], matrix[j][i]

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信