Leetcode Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?
思路:坐标是i,j的元素旋转90坐标变成了j,n-i+1
首先将矩阵倒置,那么i,j元素会变成n-i+1,j. 如果想变成j,n-i+1。 则直接交换就ok
AC 代码
class Solution: # @param matrix, a list of lists of integers # @return a list of lists of integers def rotate(self, matrix): matrix.reverse() length = len(matrix) for i in range(0,length): for j in range(i+1, length): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] return matrix

浙公网安备 33010602011771号