标签:描述 return rev info anything lis 旋转 map reverse
题目描述:
方法一:先转置再反转
class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ n = len(matrix) for i in range(n): for j in range(i,n): matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j] for i in range(n): matrix[i].reverse() return
方法二:
matrix[:] = list(map(lambda x:list(x)[::-1], zip(*matrix)))
方法三:
class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ n = len(matrix[0]) for i in range(n // 2 + n % 2): for j in range(n // 2): tmp = matrix[n - 1 - j][i] matrix[n - 1 - j][i] = matrix[n - 1 - i][n - j - 1] matrix[n - 1 - i][n - j - 1] = matrix[j][n - 1 -i] matrix[j][n - 1 - i] = matrix[i][j] matrix[i][j] = tmp
标签:描述 return rev info anything lis 旋转 map reverse
原文地址:https://www.cnblogs.com/oldby/p/11174853.html