[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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- nodejs XML和json互相转换
Docs: https://www.npmjs.com/package/fast-xml-parser const xml = ` <user> <name>ajanuw< ...
- PAT甲级1061 Dating
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805411985604608 题意: 给定四个字符串. 前两个字符串 ...
- ==和equal()的区别
“==”比较的是对象引用的地址相不相同 “equal()”比较的是内容是否相等
- 1.7Oob成员变量和局部变量疑难区分
import java.util.Scanner; public class booleann { private float fWidth; private float fHeight; void ...
- Python:time模块、calendar模块
time模块 import time 获取时间戳 >>>time.time() #1532418950.7246091 获取时间元组 >>> time.localt ...
- 不可访问内存 Java四种引用包括强引用,软引用,弱引用,虚引用
小结: 1.不可访问内存是指一组没有任何可访问指针指向的由计算机程序进行动态分配的内存块. 2.垃圾收集器能决定是否一个对象还是可访问的:任何被确定不可访问的对象将会被释放. https://zh.w ...
- php$_SERVER['SCRIPT_NAME']和__FILE__的区别
$_SERVER['SCRIPT_FILENAME'] -------> 当前执行程序的绝对路径及文件名__FILE__ ...
- saltstack---自动化运维平台
https://github.com/ixrjog/adminset[自动化运维平台:CMDB.CD.DevOps.资产管理.任务编排.持续交付.系统监控.运维管理.配置管理 ] https://ww ...
- .net Core2建立MVC网站,部署
1..net Core2使用sqlservver.EFCore,部署在linux上将出错,具体是错原因大概是:连接超时的意思.=>就想测试下linux到底能不能连接sqlserver.是两者技术 ...
- 什么是渲染目标(render target)&& 渲染到纹理(Render To Texture, RTT)详解
渲染到纹理(Render To Texture, RTT)详解 RTT是现在很多特效里面都会用到的一项很基本的技术,实现起来很简单,也很重要.但是让人不解的是网上搜索了半天只找到很少的文章说这个事儿, ...