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.
就是给二维数组做8方向均值平缓
class Solution(object): def imageSmoother(self, M): """ :type M: List[List[int]] :rtype: List[List[int]] """ dires = [[-1, 0], [-1, -1], [0, -1], [1, -1], [1, 0], [1, 1], [0, 1], [-1, 1]] matrix = [] n = len(M) m = len(M[0]) for i in range(n): matrix.append([0] * m) for i in range(n): for j in range(m): cnt = 1 total_sum = M[i][j] for dire in dires: new_i = i + dire[0] new_j = j + dire[1] if new_i >= 0 and new_i < n and new_j >= 0 and new_j < m: cnt += 1 total_sum += M[new_i][new_j] matrix[i][j] = int(floor(total_sum / cnt)) return matrix