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].

原题地址: 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的更多相关文章

  1. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  2. 【Leetcode_easy】661. Image Smoother

    problem 661. Image Smoother 题意:其实类似于图像处理的均值滤波. solution: 妙处在于使用了一个dirs变量来计算邻域数值,看起来更简洁! class Soluti ...

  3. [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 ...

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

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

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

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

  6. LeetCode - 661. Image Smoother

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

  7. 661. Image Smoother色阶中和器

    [抄题]: Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...

  8. 661. Image Smoother

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. Leetcode with Python -> Array

    118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

随机推荐

  1. C# interface 的特性 无法被implement class继承

    最近做interface添加特性后,implement class 无法继承. 微软要求class是实现Interface而不是继承,所以我们必须手动添加特性,而不能自动继承. 对于abstract ...

  2. 【OpenJ_Bailian - 2795】金银岛(贪心)

    金银岛 Descriptions: 某天KID利用飞行器飞到了一个金银岛上,上面有许多珍贵的金属,KID虽然更喜欢各种宝石的艺术品,可是也不拒绝这样珍贵的金属.但是他只带着一个口袋,口袋至多只能装重量 ...

  3. (转)cookie和session的区别

    转自 http://www.cnblogs.com/shiyangxt/archive/2008/10/07/1305506.html http://justsee.iteye.com/blog/15 ...

  4. Java 序列化(转)

    转自 http://www.infoq.com/cn/articles/cf-java-object-serialization-rmi 对于一个存在于Java虚拟机中的对象来说,其内部的状态只保持在 ...

  5. MySQL习题1 一对多实例 产品和分类

    /* 需求:建立产品和分类表 1.查询每种分类的产品数量,没有产品的分类也要统计.(cname,quantity) 2.根据分类名称查询分类中的所有产品 */ -- ----------------- ...

  6. self.tabBarController.selectedIndex

    KindViewController *vc =((UINavigationController *) [self.tabBarController viewControllers][]).viewC ...

  7. 模拟ssh、黏包、hashlib模块(MD5)

    待补充..... 一.模拟ssh 二.黏包 1.黏包现象 让我们基于tcp先制作一个远程执行命令的程序(命令ls -l ; lllllll ; pwd) res=subprocess.Popen(cm ...

  8. 使用Yeoman 创建 angular应用

    一.安装 Yeoman npm install yo -g 如果提示当前nodejs版本和npm版本太低,先升级下再安装yeoman. 安装成功后,默认只有webapp和Mocha这两个生成器. 二. ...

  9. 05.Javascript——入门函数

    //定义函数的方法1 function abs(x) { if (x >= 0) { return x; } else { return -x; } } 上述abs()函数的定义如下: func ...

  10. Spring Security – security none, filters none, access permitAll

    1.概述 Spring Security提供了几种将请求模式配置为不安全或允许所有访问的机制.取决于这些机制中的哪一种 - 这可能意味着根本不在该路径上运行安全过滤器链,或者运行过滤器链并允许访问 2 ...