leetcode | 图片的旋转(顺时针90°) | Python

来源:互联网 发布:苹果手机怎么关4g网络 编辑:程序博客网 时间:2024/05/07 13:21

题目:

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

class Solution(object):    def rotate(self, matrix):        """        :type matrix: List[List[int]]        :rtype: void Do not return anything, modify matrix in-place instead.        """        res = []                while matrix[0]:            tmp = []            for row in matrix:                                                                tmp.insert(0,row.pop(0))   #list.insert(position,value)                         res.append(tmp)        for i in range(len(matrix)): #这里,必须对matrix中的每行都进行修改,才能对其重新赋值,不能直接:matrix=res            matrix[i] = res[i]                        s = Solution()matrix = [[1,2],[3,4]]s.rotate(matrix)print matrix



原创粉丝点击