LeetCode 661 : Image Smoother(python)

7 篇文章 0 订阅
2 篇文章 0 订阅

好久没更新博客了,最近沉迷于Android和深度学习的研究,python、c++、java等语言都会接触,难免会把一些语法层面的东西记混了。也很久没上Leetcode,感觉现在脑子不够用,只够刷Easy的题。周末闲来无聊逛了一下,看到一道平滑滤波的题。没想到Leetcode也这么与时俱进,竟然出现了图像处理的相关题目。不过由于对python列表复制的不熟,还是成功跳进了一个坑。仅以此文做一些总结。

原题:
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.

Example 1:
Input:
[[1,1,1],
[1,0,1],
[1,1,1]]
Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0

Note:
The value in the given matrix is in the range of [0, 255].
The length and width of the given matrix are in the range of [1, 150].

解题过程:
一开始一上来觉得很简单,先写个滤波器,然后从左至右,从上至下移动,有一点3*3卷积的feel。当然这中间要判断滤波器覆盖的点是否合法,于是又写了个判断点是否合法的函数。当然,这样的思路是木有问题的。先贴上代码:

class Solution:
    def isLegal(self,x,y,i,j):
        return (True if((i<x)and(j<y)and(i>=0)and(j>=0)) else False)

    def imageSmoother(self, M):
        """
        :type M: List[List[int]]
        :rtype: List[List[int]]
        """
        x=len(M)
        y=len(M[0])
        returnM=M
        Filter=[[-1,-1],[-1,0],[-1,1],[0,-1],[0,0],[0,1],[1,-1],[1,0],[1,1]]
        for i in range(x):
            for j in range(y):
                legalPointNum=0
                graySum=0
                for fi in range(9):
                        tmpi=i+Filter[fi][0]
                        tmpj=j+Filter[fi][1]
                        if(self.isLegal(x,y,tmpi,tmpj)):
                            graySum+=M[tmpi][tmpj]
                            legalPointNum+=1
                returnM[i][j]=int(graySum/legalPointNum)
        return returnM

结果,一提交,在第12个样例就卡住了:

Your input

[[2,3,4],[5,6,7],[8,9,10],[11,12,13],[14,15,16]]
Your answer

[[4,4,5],[6,6,6],[8,9,9],[11,11,12],[12,12,12]]
Expected answer

[[4,4,5],[5,6,6],[8,9,9],[11,12,12],[13,13,14]]

来来回回检查了好几遍代码,发现看不出有什么问题,结果就各种print,如下:

class Solution:
    def isLegal(self,x,y,i,j):
        return (True if((i<x)and(j<y)and(i>=0)and(j>=0)) else False)

    def imageSmoother(self, M):
        """
        :type M: List[List[int]]
        :rtype: List[List[int]]
        """
        x=len(M)
        y=len(M[0])
        returnM=M
        Filter=[[-1,-1],[-1,0],[-1,1],[0,-1],[0,0],[0,1],[1,-1],[1,0],[1,1]]
        for i in range(x):
            for j in range(y):
                legalPointNum=0
                graySum=0
                for fi in range(9):
                        tmpi=i+Filter[fi][0]
                        tmpj=j+Filter[fi][1]
                        if(self.isLegal(x,y,tmpi,tmpj)):
                            print(tmpi)
                            print(tmpj)
                            graySum+=M[tmpi][tmpj]
                            legalPointNum+=1
                print(graySum)
                print(legalPointNum)
                returnM[i][j]=int(graySum/legalPointNum)
        return returnM

果然发现当i=0,j=1的时候就错了,结果来来回回检查代码,感觉returnM=M 有问题,于是考虑到肯定是list复制的问题了,目测这么写returnM和M的地址是一样的,结果print了一下id,果然是一样:

print(id(returnM))
print(id(M))

结果老老实实跑去google,发现了这篇博文的总结:
http://www.cnblogs.com/ifantastic/p/3811145.html
原来如此!
改过来之后就AC了:

class Solution:
    def isLegal(self,x,y,i,j):
        return (True if((i<x)and(j<y)and(i>=0)and(j>=0)) else False)

    def imageSmoother(self, M):
        """
        :type M: List[List[int]]
        :rtype: List[List[int]]
        """
        x=len(M)
        y=len(M[0])
        returnM=copy.deepcopy(M)
        # returnM=M
        Filter=[[-1,-1],[-1,0],[-1,1],[0,-1],[0,0],[0,1],[1,-1],[1,0],[1,1]]
        for i in range(x):
            for j in range(y):
                legalPointNum=0
                graySum=0
                for fi in range(9):
                        tmpi=i+Filter[fi][0]
                        tmpj=j+Filter[fi][1]
                        if(self.isLegal(x,y,tmpi,tmpj)):
                            # print(tmpi)
                            # print(tmpj)
                            graySum+=M[tmpi][tmpj]
                            legalPointNum+=1
                # print(graySum)
                # print(legalPointNum)
                returnM[i][j]=int(graySum/legalPointNum)
        return returnM

#没过的样例
#[[2,3,4],[5,6,7],[8,9,10],[11,12,13],[14,15,16]]
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值