[LeetCode] 661. Image Smoother_Easy
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:
- The value in the given matrix is in the range of [0, 255].
- 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的更多相关文章
- LeetCode 661. Image Smoother (图像平滑器)
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- Java实现 LeetCode 661 图片平滑器(暴力)
661. 图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元 ...
- LeetCode - 661. Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- Leetcode 661.图片平滑器
图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个 ...
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- 【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】661. Image Smoother 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...
- [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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- linux 修改hosts文件
1.修改hostssudo gedit /etc/hosts2.添加解析记录( . )完整案例:127.0.0.1 localhost.localdomain localhost简洁记录:127.0. ...
- {MySQL存储引擎介绍}一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用
MySQL存储引擎介绍 MySQL之存储引擎 本节目录 一 存储引擎解释 二 MySQL存储引擎分类 三 不同存储引擎的使用 一 存储引擎解释 首先确定一点,存储引擎的概念是MySQL里面才有的,不是 ...
- instrument之Time Profiler总结
一.工欲善其事必先利其器 time profile时间分析工具用来检测应用CPU的使用情况,可以看到应用程序中各个方法正在消耗CPU时间,使用大量CPU不一定是个问题.客户端中动画就对CPU依赖就非常 ...
- [DPI][suricata] suricata 配置使用
前文: [DPI] suricata-4.0.3 安装部署 至此, 我们已经拥有了suricata可以运行的环境了. 接下来,我们来研究一下它的功能, 首先,分析一下配置文件: /suricata/e ...
- cruise-control
https://github.com/linkedin/cruise-control https://engineering.linkedin.com/blog/2017/08/open-sourci ...
- mysql学习【第4篇】:MySQL函数和编程
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! mysql学习[第4篇]:MySQL函数 官方文档 : 官方文档 常用函数 分类: 数学函数 , ...
- LeetCode 985 Sum of Even Numbers After Queries 解题报告
题目要求 We have an array A of integers, and an array queries of queries. For the i-th query val = queri ...
- HTML中select的option设置selected="selected"无效的解决方案
今天遇到了一个奇葩问题,写HTML时有个select控件,通过设置option的selected="selected"居然无效,但是在其他浏览器是可以的,问了一下Google大神, ...
- 新建oracle用户
1.oracle 账户登录linux:2.如果存在多个切换实例:命令:export ORACLE_SID=实例名:如:export ORACLE_SID=utf81863.切换至管理员账户:sqlpl ...
- TZOJ 3198: 区间和
描述 给定n个数据,有两个操作,加减其中的一个数据,当然还可查询在某段数据的和. 输入 输入数据有多组,每组数据的第一行输入n,1=<n<=500000,代表数据的个数.第二行输入具体数据 ...