Leetcode刷题笔记

第十四天 Flipping an Image

2018-09-02  本文已影响0人  业余马拉松选手

刷刷水题,还是很开心滴

https://leetcode-cn.com/problems/flipping-an-image/description/

反转一张图片,优先倒序,然后翻转,几乎是直译这个算法,这里继续使用了下Python的“三元”运算符,虽然用起来有点怪,但多练练吧

代码确实很简单,直接写吧:

class Solution:
    def flipAndInvertImage(self, A):
        """
        :type A: List[List[int]]
        :rtype: List[List[int]]
        """
        ret = []
        for i in range(len(A)):
            temp1 = list(reversed(A[i]))
            temp2 = list(map(lambda x: 0 if x != 0 else 1,temp1))
            ret.append(temp2)
        return ret

既然算法上没有什么特别点,那么就记录一些Python的“语法点”吧:
1、 list的用法
2、 reversed的用法
3、 map函数以及lambda的用法
4、 Python的“三元运算符”

因为家里有点事情,还是缺乏总结,哎

上一篇 下一篇

猜你喜欢

热点阅读