https://leetcode-cn.com/problems/rotate-image/submissions/
90度 旋转之后 row, col = col, n-1-row,
可以直接便利存储到新的矩阵中。
由于题目要求不能使用新的矩阵来存放变换之后的数据,
所以需要用对折+对角线对折来处理
对折: row, col = n-1-row, col
对角线: n-1-row, col = col, n-1-row
对折遍历上半部分,对角线便利左下部分
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
# 90度旋转可以转化为上下折叠+对角线折叠
col = row = len(matrix)
need_to_rotate = row // 2
for r in range(need_to_rotate):
for c in range(col):
print('row=%s, col=%s' % (r, c))
print(row-1-r, c)
matrix[r][c], matrix[row-1-r][c] = matrix[row-1-r][c], matrix[r][c]
for r in range(row):
for c in range(col):
if c < r:
matrix[r][c], matrix[c][r] = matrix[c][r], matrix[r][c]
return matrix
网友评论