美文网首页Leetcode刷题笔记
第十四天 Flipping an Image

第十四天 Flipping an Image

作者: 业余马拉松选手 | 来源:发表于2018-09-02 23:55 被阅读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的“三元运算符”

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

相关文章

网友评论

    本文标题:第十四天 Flipping an Image

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