LeetCode 832 Flipping an Image 解题报告

题目要求

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.

To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].

To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].

题目分析及思路

题目给出一个矩阵A,其中的元素是0或1。要求先反转每一行,然后再将1变成0,0变成1。最后返回结果矩阵。可以先逆序,然后用异或操作。

python代码​

class Solution:

    def flipAndInvertImage(self, A):

        """

        :type A: List[List[int]]

        :rtype: List[List[int]]

        """

        rows = len(A)

        cols = len(A[0])

        for row in range(rows):

            A[row] = A[row][::-1]

            for col in range(cols):

                A[row][col] ^= 1

        return A

        

 

posted on 2019-01-06 21:41  锋上磬音  阅读(99)  评论(0编辑  收藏  举报