661. Image Smoother@python
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].
原题地址: Image Smoother
难度: Easy
题意: 平滑图片, 每一个点值为其四周(包含自身)的平均值
思路:
一个二维数组, 按照题意,点可以分为三类
(1)四个角的点, 其周围有4个点(包含自身)
(2)二维数组最外层除了四个角的点,其周围有6个点(包含自身)
(3)其他的点(内层点),其周围有9个点(包含自身)
直接暴力解决,遍历数组
代码:
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
m = len(M)
n = len(M[0])
res = [[0] * n for i in range(m)]
if m <= 1 and n <= 1:
return M
if m == 1 and n > 1:
res[0][0] = (M[0][0] + M[0][1]) // 2
res[0][-1] = (M[0][-1] + M[0][-2]) // 2
for j in range(1, n-1):
res[0][j] = sum(M[0][j-1: j+2]) // 3
return res if n == 1 and m > 1:
res[0][0] = (M[0][0] + M[1][0]) // 2
res[-1][0] = (M[-1][0] + M[-2][0]) // 2
for i in range(1, m-1):
res[i][0] = (M[i-1][0] + M[i][0] + M[i+1][0]) // 3
return res for i in range(m):
for j in range(n):
if i == 0 and j == 0:
res[i][j] = (M[i][j] + M[i][j+1] + M[i+1][j] + M[i+1][j+1]) // 4
if i == 0 and j == n-1:
res[i][j] = (M[i][j] + M[i][j-1] + M[i+1][j] + M[i+1][j-1]) // 4
if i == m-1 and j == 0:
res[i][j] = (M[i][j] + M[i][j+1] + M[i-1][j] + M[i-1][j+1]) // 4
if i == m-1 and j == n-1:
res[i][j] = (M[i][j] + M[i][j-1] + M[i-1][j] + M[i-1][j-1]) // 4 if i == 0 and 0 < j < n-1:
res[i][j] = (sum(M[i][j-1: j+2]) + sum(M[i+1][j-1: j+2])) // 6
if i == m-1 and 0 < j < n-1:
res[i][j] = (sum(M[i][j-1: j+2]) + sum(M[i-1][j-1: j+2])) // 6 if j == 0 and 0 < i < m-1:
res[i][j] = (sum(M[i][j: j+2]) + sum(M[i-1][j: j+2]) + sum(M[i+1][j: j+2])) // 6 if j == n-1 and 0 < i < m-1:
res[i][j] = (sum(M[i][j-1: j+1]) + sum(M[i-1][j-1: j+1]) + sum(M[i+1][j-1: j+1])) // 6 if 0 < i < m -1 and 0 < j < n-1:
res[i][j] = (sum(M[i][j-1: j+2]) + sum(M[i-1][j-1: j+2]) + sum(M[i+1][j-1: j+2])) // 9 return res
时间复杂度: O(mn)
空间复杂度: O(1)
661. Image Smoother@python的更多相关文章
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 【Leetcode_easy】661. Image Smoother
problem 661. Image Smoother 题意:其实类似于图像处理的均值滤波. solution: 妙处在于使用了一个dirs变量来计算邻域数值,看起来更简洁! class Soluti ...
- [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 ...
- 【LeetCode】661. Image Smoother 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...
- 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. Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- 661. Image Smoother色阶中和器
[抄题]: Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...
- 661. Image Smoother
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- Leetcode with Python -> Array
118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
随机推荐
- 51nod1475(贪心&枚举)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1475 题意:中文题诶- 思路:看懂题意后,首先想到的是贪心: ...
- 1.python真的是万恶之源么?(初识python)
python真的是万恶之源么? 计算机基础及puthon了解 1.计算机基础知识 cpu : 相当于人类大脑,运算和处理问题 内存 : 临时存储数据,单点就消失,4G,8G,16G,32G 硬盘 : ...
- mysql--浅谈查询1
这是对自己学习燕十八老师mysql教程的总结,非常感谢燕十八老师. 依赖软件:mysql5.6 系统环境:win 在谈查询之前,先说一个特别重要的概念 一定将列名看成变量,既然是变量就可以运算 一定将 ...
- 干货:排名前16的Java工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- hibernate添加帮助文档和源码
- djangoXadmin
是一个基于admin二次开发的开源组件,但是貌似已经停止开发了. 安装方式:(py3.6,django2.1) 1 先用pip安装xadmin2,它会安装xadmin和一些依赖包 2 用pip卸载xa ...
- 洛谷P1823 [COI2007] Patrik 音乐会的等待
https://www.luogu.org/problemnew/show/P1823 自己只会一个log的 设取的人的位置分别是l,r(l<r) 这个做法大概是考虑枚举r,设法对于每个r求出有 ...
- Jasper_crosstab_measure_display a value of field in crosstab total row
1.create a measure <measure name="myField" class="java.lang.String"> <m ...
- python入门之实例-用户登录、注册
用户密码存储文件db(其中用户和密码之间用$符合隔开): admin$123456 root$sdfk9f24 chy$654321 代码如下: def login(username,password ...
- python_22(Form-CRM)
第1章 CRM 1.1 建项目 1.2 settings1.3 规范url 1.4 公共的后台模板1.5 创建部门表 1.6 建库移库 1.7 母版继承 1.7.1 导入static 1.7.2 导入 ...