美文网首页
每天(?)一道LeetCode(8) Rotate Image

每天(?)一道LeetCode(8) Rotate Image

作者: 失业生1981 | 来源:发表于2019-01-22 21:43 被阅读0次

    Array

    048. Rotate Image

    You are given an n x n 2D matrix representing an image.
    Rotate the image by 90 degrees (clockwise).
    Note:
    You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.


    对给定矩阵进行旋转,但是不能用额外的空间

    Solutions

    很简单的想法:
    先转置,a[i][j]a[j][i]互换
    然后对每一行元素取反

    class Solution:
        def rotate(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: void Do not return anything, modify matrix in-place instead.
            """
            for i in range(len(matrix)):
                for j in range(i,len(matrix)):
                    matrix[i][j],matrix[j][i]=matrix[j][i],matrix[i][j]
                matrix[i].reverse()
    

    相关文章

      网友评论

          本文标题:每天(?)一道LeetCode(8) Rotate Image

          本文链接:https://www.haomeiwen.com/subject/akzrjqtx.html