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.

Example 1:

Input:
[[1,1,1],
[1,0,1],
[1,1,1]]
Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0

Note:

  1. The value in the given matrix is in the range of [0, 255].
  2. The length and width of the given matrix are in the range of [1, 150].

思路就是正常的, 两个for loop, 然后将8个邻居和自己相加取平均数, 最后代替原来的数即可.

T: O(m,n)   S; O(1)

Code

class Solution:
def imageSmoother(self, M):
dirs, lrc = [(x, y) for x in range(-1,2) for y in range(-1,2)], [len(M), len(M[0])]
ans = [[0]* lrc[1] for _ in range(lrc[0])]
def sum2(i, j):
ans, count = 0, 0
for c1, c2 in dirs:
nr, nc = i + c1, j + c2
if 0 <= nr < lrc[0] and 0 <= nc < lrc[1]:
count += 1
ans += M[nr][nc]
return ans//count
for i in range(lrc[0]):
for j in range(lrc[1]):
ans[i][j] = sum2(i,j)
return ans

[LeetCode] 661. Image Smoother_Easy的更多相关文章

  1. LeetCode 661. Image Smoother (图像平滑器)

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  2. Java实现 LeetCode 661 图片平滑器(暴力)

    661. 图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元 ...

  3. LeetCode - 661. Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  4. Leetcode 661.图片平滑器

    图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个 ...

  5. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  6. 【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  7. 【LeetCode】661. Image Smoother 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...

  8. [LeetCode&Python] Problem 661. Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 第一章:初识Python

    一个Python列表 movies = ["The Holy Grail",1975,"Terry Jones&Terry Gilliam",91,[& ...

  2. SQL中的between and

    "between xx and  xx "在SQL中的用法 这个大家都很熟悉,但是当问到是否包含两端的值时,就有点儿不确定了.在W3School网站上,有这样的一段话: 参考:ht ...

  3. TOP100summit:【分享实录-美团点评】 业务快速升级发展背后的系统架构演进

    本篇文章内容来自2016年TOP100summit美团●大众点评高级技术专家,酒店后台研发组eHome团队负责人许关飞的案例分享.编辑:Cynthia 许关飞:美团●大众点评高级技术专家,酒店后台研发 ...

  4. innodb表锁情况

    MySQL InnoDB默认行级锁.行级锁都是基于索引的 行级锁变为表级锁情况如下: 1.如果一条SQL语句用不到索引是不会使用行级锁的,会使用表级锁把整张表锁住. 2.表字段进行变更. 3.进行整表 ...

  5. [qemu] qemu旧的net参数已经不再可用了,新的这样用。

    老的命令: /root/BUILD_qemu/bin/qemu-system-x86_64 -enable-kvm \ -m 2G \ -cpu Nehalem -smp cores=,threads ...

  6. 树形dp的深入讨论

    越发向dp深入越发现dp越有意思! 这道题做的时候感觉十分的难,然后看完学长的题解恍然大悟.设状态不好导致想了一中午,一直感觉不可做,其实是自己的状态设的不对,这道题呢,首先是一个求在树上建多个厂,而 ...

  7. php面向对象的封装性

    面向对象编程 1:封装性 访问修饰符,作用为封装,防止外部访问. public 公有的 private 私有的 protected 受保护的 一开始具体也没搞明白是怎么回事,搞个小的Demo就出来了 ...

  8. idea的基本使用

    对于Idea没有workspace的概念,但是它把每个项目都看成是maven的一个模块,在一个idea窗口要想显示多个项目时就和eclipse不太一样,下面会详细介绍. 另外maven的setting ...

  9. LDA学习小记

    看到一段对主题模型的总结,感觉很精辟: 如何找到文本隐含的主题呢?常用的方法一般都是基于统计学的生成方法.即假设以一定的概率选择了一个主题,然后以一定的概率选择当前主题的词.最后这些词组成了我们当前的 ...

  10. C# cmd bcp 导出数据

    背景需求:应用系统间数据自动同步处理,要求高效无人工干预 技术实现:C#启动cmd,通过BCP命令传入必要参数,实现数据导出 /// <summary> /// cmd下,启动应用程序命令 ...