every blog every motto: You will never know unless you try
0. 前言
生活好难,再坚持坚持!
1. 正文
1.2 题解
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
length = len(matrix) # 长和宽相同
for o_r in range(length // 2):
for o_c in range((length+1) //2):
temp = matrix[o_r][o_c]
# -------------------------
# 旋转发生如下变化:
# 1. 原来的列变成了新的行
# 2. 原来的行+新的列=length-1
# -------------------------
new_row = length - o_c - 1 # 行
new_col = length - o_r - 1 # 列
matrix[o_r][o_c] = matrix[new_row][o_r]
matrix[new_row][o_r] = matrix[new_col][new_row]
matrix[new_col][new_row] = matrix[o_c][new_col]
matrix[o_c][new_col] = temp