[LeetCode&Python] Problem 661. Image Smoother

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:

  1. The value in the given matrix is in the range of [0, 255].
  2. The length and width of the given matrix are in the range of [1, 150].
  3. class Solution(object):
        def imageSmoother(self, M):
            """
            :type M: List[List[int]]
            :rtype: List[List[int]]
            """
            ans=[]
            r=[]
            m=len(M)
            n=len(M[0])
            
            if (m==0 and n==0) or (m==1 and n==1):
                return M
            elif m==1:
                for j in range(n):
                    if j==0:
                        r.append((M[0][j]+M[0][j+1])//2)
                    elif j==n-1:
                        r.append((M[0][j]+M[0][j-1])//2)
                    else:
                        r.append((M[0][j]+M[0][j+1]+M[0][j-1])//3)
                ans.append(r)
                return ans
            elif n==1:
                for i in range(m):
                    if i==0:
                        r.append((M[i][0]+M[i+1][0])//2)
                        ans.append(r)
                        r=[]
                    elif i==m-1:
                        r.append((M[i][0]+M[i-1][0])//2)
                        ans.append(r)
                        r=[]
                    else:
                        r.append((M[i][0]+M[i+1][0]+M[i-1][0])//3)
                        ans.append(r)
                        r=[]
                return ans
            else:            
                for i in range(m):
                    for j in range(n):
                        if i==0:
                            if j==0:
                                r.append((M[i][j]+M[i+1][j]+M[i+1][j+1]+M[i][j+1])//4)
                            elif j==n-1:
                                r.append((M[i][j]+M[i+1][j]+M[i+1][j-1]+M[i][j-1])//4)
                            else:
                                r.append((M[i][j]+M[i+1][j]+M[i+1][j+1]+M[i][j+1]+M[i+1][j-1]+M[i][j-1])//6)
                        elif i==m-1:
                            if j==0:
                                r.append((M[i][j]+M[i-1][j]+M[i-1][j+1]+M[i][j+1])//4)
                            elif j==n-1:
                                r.append((M[i][j]+M[i-1][j]+M[i-1][j-1]+M[i][j-1])//4)
                            else:
                                r.append((M[i][j]+M[i-1][j]+M[i-1][j+1]+M[i][j+1]+M[i-1][j-1]+M[i][j-1])//6)
                        else:
                            if j==0:
                                r.append((M[i][j]+M[i][j+1]+M[i-1][j]+M[i-1][j+1]+M[i+1][j]+M[i+1][j+1])//6)
                            elif j==n-1:
                                r.append((M[i][j]+M[i][j-1]+M[i-1][j]+M[i+1][j]+M[i-1][j-1]+M[i+1][j-1])//6)
                            else:
                                r.append((M[i][j]+M[i][j-1]+M[i-1][j]+M[i+1][j]+M[i-1][j-1]+M[i+1][j-1]+M[i][j+1]+M[i-1][j+1]+M[i+1][j+1])//9)
                    ans.append(r)
                    r=[]
                return ans
    

      

猜你喜欢

转载自www.cnblogs.com/chiyeung/p/10071373.html