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. 安装APK时引发INSTALL_PARSE_FAILED_MANIFEST_MALFORMED错误的几种可能(申明:来源于网络)

    安装APK时引发INSTALL_PARSE_FAILED_MANIFEST_MALFORMED错误的几种可能(申明:来源于网络) 地址:https://my.oschina.net/freestyle ...

  2. Nodejs----注册登录

    这个小应用使用到了node.js  bootstrap  express  以及数据库的操作 :使用mongoose对象模型来操作 mongodb 如果没了解过的可以先去基本了解一下相关概念~ 首先注 ...

  3. js事件绑定的几种方式

    在JavaScript中,有三种常用的绑定事件的方法: 在DOM元素中直接绑定: 在JavaScript代码中绑定: 绑定事件监听函数 一. 在DOM元素中直接绑定 这里的DOM元素,可以理解为HTM ...

  4. IIS8.5 Error Code 0x8007007e HTTP 错误 500.19的解决方法

    window server 2012R2 IIS8.5 引用:https://www.52jbj.com/yunying/340443.html HTTP 错误 500.19 - Internal S ...

  5. 一些shell默认的变量

    测试脚本如下,我这里主要想测试$0,$1,$2,$n,$@,$*默认都代表了什么? #!/bin/sh echo '$1='$1 echo '$2='$2 echo '$@='$@ echo '$*= ...

  6. inotifywait实现目录监控--http://man.linuxde.net/inotifywait

    sudo apt install inotify-tools while inotifywait -q -r -e create,delete,modify,move,attrib --exclude ...

  7. python fabric实现远程操作和部署示例

    https://www.jb51.net/article/48434.htm 近期接手越来越多的东西,发布和运维的工作相当机械,加上频率还蛮高,导致时间浪费还是优点多.修复bug什么的,测试,提交版本 ...

  8. 【转】escape()、encodeURI()、encodeURIComponent()区别详解

    escape().encodeURI().encodeURIComponent()区别详解 原文链接:http://www.cnblogs.com/tylerdonet/p/3483836.html ...

  9. 内部排序->归并排序->2-路归并排序

    文字描述 假设初始序列有n个记录,则可看成是n个有序的字序列,每个字序列的长度为1,然后两两归并,得到[n/2]个长度为2或1的有序子序列:再两两归并,…, 如此重复,直到得到一个长度为n的有序序列为 ...

  10. python摸爬滚打之day01----初识Python

    1.编程语言分类 编译型语言:程序被一次性全部翻译成机器语言,计算机直接以机器语⾔言来运⾏行行此程序. 优点:运行效率高,可脱离语言环境独立运行. 缺点:开发效率低,可移植性差. 解释型语言:将程序逐 ...