832. Flipping an Image

题目来源:
 
自我感觉难度/真实难度:
 
题意:
 
分析:
 
自己的代码:
class Solution(object):
    def flipAndInvertImage(self, A):
        """
        :type A: List[List[int]]
        :rtype: List[List[int]]
        """
        B=[]
        row=len(A)
        col=len(A[0])
        for i in range(row):
            B.append(A[i][::-1])
            for j in range(col):
                B[i][j]^=1
        return B

 

代码效率/结果:

Runtime: 52 ms, faster than 97.21% of Python online submissions forFlipping an Image.

 
优秀代码:
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

 

代码效率/结果:
 
自己优化后的代码:
 
反思改进策略:

1.0和1取反,可以使用异或1

2.可以直接对原来的list进行操作,省的重新定义list

 

posted @ 2019-01-13 22:26  dgi  阅读(121)  评论(0编辑  收藏  举报