您的位置:首页 > 编程语言 > Python开发

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

2017-06-05 22:18 453 查看
题目:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: