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

42 leetcode - Rotate Image

2016-12-16 11:37 330 查看
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).#矩阵旋转90度
Follow up:
Could you do this in-place?不使用额外空间,以a[0][n-1]与a[n-1][0]对角线为轴,将矩阵对称交换,然后将列反转
'''
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
length = len(matrix)
if length <= 1:
return

for row in range(length - 1):
for col in range(length - 1 - row):
#print row,col,'->',length - 1 - col,length - 1 - row
tmp = matrix[row][col]
matrix[row][col] = matrix[length - 1 - col][length - 1 - row]
matrix[length - 1 - col][length - 1 - row] = tmp

for col in range(length):
left,right = 0,length - 1
while left < right:
tmp = matrix[left][col]
matrix[left][col] = matrix[right][col]
matrix[right][col] = tmp
left += 1
right -= 1

if __name__ == "__main__":
s = Solution()
a = [range(1,4),range(4,7),range(7,10)]
print a
s.rotate([1])
print a
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息