题目描述
二维整数矩阵 M 表示一个图片的灰度。设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下取整) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们。
注意:给定矩阵中的整数范围为 [0, 255]。矩阵的长和宽的范围均为 [1, 150]。
示例
输入:
[[1,1,1],
[1,0,1],
[1,1,1]]
输出:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
解释:
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
解答
开始把题目理解错了,直接在周围补0,然后遍历原始数组,再除以9。而题目中被除的应当是本身数组中包含的元素
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
row = len(M) # 原始行数
col = len(M[0]) # 原始列数
# 四周补0
padding = [0 for i in range(col)]
M.insert(0,padding)
M.append(padding)
for i in range(0,row+1):
M[i].insert(0,0)
M[i].insert(col+1,0) # 已经插入了一个0,最后的索引改变了
new_M = list()
for i in range(1,row+1):
new_M.append(list())
for j in range(1,col+1):
sub_M_sum = M[i-1][j-1]+M[i-1][j]+M[i-1][j+1]+M[i][j-1]+M[i][j]+M[i][j+1]\
+M[i+1][j-1]+M[i+1][j]+M[i+1][j+1]
sub_M_mean = int(sub_M_sum*1.0/9)
new_M[i-1].append(sub_M_mean)
return new_M
按题目要求来,暴力解决,然而情况太多,有些情况没有考虑到,比如[[1]]
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
row = len(M) # 原始行数
col = len(M[0]) # 原始列数
new_M = list()
for i in range(0,row):
new_M.append(list())
for j in range(0,col):
if i==0 and j==0: # 左上
sub_M_sum = M[i][j]+M[i][j+1]+M[i+1][j]+M[i+1][j+1]
sub_M_mean = int(sub_M_sum*1.0/4)
new_M[i].append(sub_M_mean)
elif i==0 and (j>0 and j<col-1): # 第一行中间
sub_M_sum = M[i][j-1]+M[i][j]+M[i][j+1]+M[i+1][j-1]+M[i+1][j]+M[i+1][j+1]
sub_M_mean = int(sub_M_sum*1.0/6)
new_M[i].append(sub_M_mean)
elif i==0 and j==col-1: # 右上
sub_M_sum = M[i][j-1]+M[i][j]+M[i+1][j-1]+M[i+1][j]
sub_M_mean = int(sub_M_sum*1.0/4)
new_M[i].append(sub_M_mean)
elif (i>0 and i<row-1) and j==0: # 第一列中间
sub_M_sum = M[i-1][j]+M[i-1][j+1]+M[i][j]+M[i][j+1]+M[i+1][j]+M[i+1][j+1]
sub_M_mean = int(sub_M_sum*1.0/6)
new_M[i].append(sub_M_mean)
elif (i>0 and i<row-1) and j==col-1: # 最后一列中间
sub_M_sum = M[i-1][j-1]+M[i-1][j]+M[i][j]+M[i][j-1]+M[i+1][j]+M[i+1][j-1]
sub_M_mean = int(sub_M_sum*1.0/6)
new_M[i].append(sub_M_mean)
elif i==row-1 and j==0: # 左下
sub_M_sum = M[i-1][j]+M[i-1][j+1]+M[i][j]+M[i][j+1]
sub_M_mean = int(sub_M_sum*1.0/4)
new_M[i].append(sub_M_mean)
elif i==row-1 and (j>0 and j<col-1): # 最后一行中间
sub_M_sum = M[i][j-1]+M[i][j]+M[i][j+1]+M[i-1][j-1]+M[i-1][j]+M[i-1][j+1]
sub_M_mean = int(sub_M_sum*1.0/6)
new_M[i].append(sub_M_mean)
elif i==row-1 and j==col-1: # 右下
sub_M_sum = M[i-1][j-1]+M[i-1][j]+M[i][j]+M[i][j-1]
sub_M_mean = int(sub_M_sum*1.0/4)
new_M[i].append(sub_M_mean)
else: # 中间
sub_M_sum = M[i-1][j-1]+M[i-1][j]+M[i-1][j+1]+M[i][j-1]+M[i][j]+M[i][j+1]+M[i+1][j-1]+M[i+1][j]+M[i+1][j+1]
sub_M_mean = int(sub_M_sum*1.0/9)
new_M[i].append(sub_M_mean)
return new_M
参考了评论区的想法:遍历数组,判断[r,c]周围的8个点是否存在,若存在,则相加并计数,最后用和除以计数值即可
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
row = len(M) # 原始行数
col = len(M[0]) # 原始列数
new_M = list()
for r in range(0,row):
new_M.append(list())
for c in range(0,col):
count = 0
sub_sum = 0
i = max([0,r-1]) # 判断行最小索引
i_max = min([r+1,row-1])
j_max = min([c+1,col-1])
while i<=i_max:
j = max([0,c-1]) # 判断列最小索引
while j<=j_max:
sub_sum = sub_sum + M[i][j]
count += 1
j += 1
i += 1
sub_mean = int(sub_sum*1.0/count)
new_M[r].append(sub_mean)
return new_M
i表示行的索引,i-1和i+1必须在[0,row-1],i最小取0,最大取row-1
j表示列的索引,j-1和j+1必须在[0,col-1],j最小取0,最大取col+1