Python Solution


  • 0
    class Solution(object):
        def imageSmoother(self, M):
            if not M:
            	return M
            h, w = len(M), len(M[0])
            ans = [[0 for i in range(w)] for j in range(h)]
            for i in range(h):
                for j in range(w):
                    s, cnt = M[i][j], 1
                    dir = [[0, 1], [0, -1], [1, 0], [-1, 0], [-1, -1], [-1, 1], [1, -1], [1, 1]]
                    for x in range(len(dir)):
                        ty, tx = i + dir[x][0], j + dir[x][1]
                        if (0 <= ty < h and 0 <= tx < w):
                            s += M[ty][tx]
                            cnt += 1
                    ans[i][j] = s / cnt
            return ans
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.