-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageSmoother.py
More file actions
24 lines (22 loc) · 783 Bytes
/
imageSmoother.py
File metadata and controls
24 lines (22 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/image-smoother/
# Author: Miao Zhang
# Date: 2021-02-26
class Solution:
def imageSmoother(self, M: List[List[int]]) -> List[List[int]]:
m = len(M)
n = len(M[0])
res = [[0 for _ in range(n)] for _ in range(m)]
dirs = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1), (0, 0)]
for i in range(m):
for j in range(n):
cnt = 0
for d in dirs:
x = i + d[0]
y = j + d[1]
if 0 <= x < m and 0 <= y < n:
res[i][j] += M[x][y]
cnt += 1
res[i][j] //= cnt
return res