832. Flipping an Image

class Solution:
    def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
        for a in A:
            first = 0
            last = len(a)-1
            while first<last:
                a[first],a[last]=1-a[last],1-a[first]
                first += 1
                last -= 1
            if len(a)&1 is 1:
                index = int((len(a)-1)/2)
                a[index] = 1-a[index]
        return A

  48ms,13.1M

优化一:

class Solution:
    def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
        return [[1-row[i] for i in range(len(row)-1,-1,-1)] for row in A]

  48ms,13.2M

 

posted @ 2019-02-27 10:43  bluedream1000  阅读(114)  评论(0编辑  收藏  举报