|
22 | 22 | # Note: |
23 | 23 | # The value in the given matrix is in the range of [0, 255]. |
24 | 24 | # The length and width of the given matrix are in the range of [1, 150]. |
25 | | - |
26 | | -class Solution(object): |
27 | | - def imageSmoother(self, M): |
28 | | - """ |
29 | | - :type M: List[List[int]] |
30 | | - :rtype: List[List[int]] |
31 | | - """ |
32 | | - def getGray(M, i, j): |
33 | | - directions = [[-1, -1], [0, -1], [1, -1], \ |
34 | | - [-1, 0], [0, 0], [1, 0], \ |
35 | | - [-1, 1], [0, 1], [1, 1]] |
36 | | - |
37 | | - total, count = 0, 0.0 |
38 | | - for direction in directions: |
39 | | - ii, jj = i + direction[0], j + direction[1] |
40 | | - if 0 <= ii < len(M) and 0 <= jj < len(M[0]): |
41 | | - total += M[ii][jj] |
42 | | - count += 1.0 |
43 | | - return int(total / count) |
44 | | - |
45 | | - result = [[0 for _ in xrange(len(M[0]))] for _ in xrange(len(M))] |
46 | | - for i in xrange(len(M)): |
47 | | - for j in xrange(len(M[0])): |
48 | | - result[i][j] = getGray(M, i, j); |
49 | | - return result |
50 | | -# Another solution with getGray function simplified. |
| 25 | + |
51 | 26 | class Solution(object): |
52 | 27 | def imageSmoother(self, M): |
53 | 28 | """ |
|
0 commit comments