码迷,mamicode.com
首页 > 编程语言 > 详细

[leetcode]Rotate Image @ Python

时间:2014-06-11 09:43:59      阅读:1264      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

原题地址:https://oj.leetcode.com/problems/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?

解题思路:先将矩阵转置,然后将矩阵的每一行翻转,就可以得到所要求的矩阵了。

代码:

bubuko.com,布布扣
class Solution:
    # @param matrix, a list of lists of integers
    # @return a list of lists of integers
    def rotate(self, matrix):
        n = len(matrix)
        for i in range(n):
            for j in range(i+1, n):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
        for i in range(n):
            matrix[i].reverse()
        return matrix
bubuko.com,布布扣

 

[leetcode]Rotate Image @ Python,布布扣,bubuko.com

[leetcode]Rotate Image @ Python

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/zuoyuan/p/3772978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!