Skip to content

Commit b776b51

Browse files
authored
Update image-smoother.py
Another solution with getGray function simplified.
1 parent 5efab83 commit b776b51

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Python/image-smoother.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,25 @@ def getGray(M, i, j):
4747
for j in xrange(len(M[0])):
4848
result[i][j] = getGray(M, i, j);
4949
return result
50+
# Another solution with getGray function simplified.
51+
class Solution(object):
52+
def imageSmoother(self, M):
53+
"""
54+
:type M: List[List[int]]
55+
:rtype: List[List[int]]
56+
"""
57+
def getGray(M, i, j):
58+
total, count = 0, 0.0
59+
for r in xrange(-1, 2):
60+
for c in xrange(-1, 2):
61+
ii, jj = i + r, j + c
62+
if 0 <= ii < len(M) and 0 <= jj < len(M[0]):
63+
total += M[ii][jj]
64+
count += 1.0
65+
return int(total / count)
66+
67+
result = [[0 for _ in xrange(len(M[0]))] for _ in xrange(len(M))]
68+
for i in xrange(len(M)):
69+
for j in xrange(len(M[0])):
70+
result[i][j] = getGray(M, i, j);
71+
return result

0 commit comments

Comments
 (0)