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
相关文章推荐
- Leetcode 48. Rotate Image
- 【Leetcode】Rotate Image
- 【leetcode】Rotate Image(middle)
- LeetCode题解——Rotate Image
- Leetcode——48. Rotate Image
- Leetcode5 Rotate Image
- LeetCode之Rotate Image
- LeetCode 48. Rotate Image
- LeetCode_rotate-image
- 【LeetCode】【Python题解】Rotate Image
- [leetcode 48] Rotate Image
- 【Leetcode】【python】Rotate Image
- [LeetCode] 48. Rotate Image 旋转图像
- LeetCode题解:Rotate Image
- [LeetCode]Rotate Image
- [LeetCode]Rotate the image(在位操作!!)
- Leetcode-48. Rotate Image
- leetcode Rotate Image
- 【Leetcode】【Medium】Rotate Image
- LeetCode 48 Rotate Image