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].
  3. class Solution(object):
    def imageSmoother(self, M):
    """
    :type M: List[List[int]]
    :rtype: List[List[int]]
    """
    ans=[]
    r=[]
    m=len(M)
    n=len(M[0]) if (m==0 and n==0) or (m==1 and n==1):
    return M
    elif m==1:
    for j in range(n):
    if j==0:
    r.append((M[0][j]+M[0][j+1])//2)
    elif j==n-1:
    r.append((M[0][j]+M[0][j-1])//2)
    else:
    r.append((M[0][j]+M[0][j+1]+M[0][j-1])//3)
    ans.append(r)
    return ans
    elif n==1:
    for i in range(m):
    if i==0:
    r.append((M[i][0]+M[i+1][0])//2)
    ans.append(r)
    r=[]
    elif i==m-1:
    r.append((M[i][0]+M[i-1][0])//2)
    ans.append(r)
    r=[]
    else:
    r.append((M[i][0]+M[i+1][0]+M[i-1][0])//3)
    ans.append(r)
    r=[]
    return ans
    else:
    for i in range(m):
    for j in range(n):
    if i==0:
    if j==0:
    r.append((M[i][j]+M[i+1][j]+M[i+1][j+1]+M[i][j+1])//4)
    elif j==n-1:
    r.append((M[i][j]+M[i+1][j]+M[i+1][j-1]+M[i][j-1])//4)
    else:
    r.append((M[i][j]+M[i+1][j]+M[i+1][j+1]+M[i][j+1]+M[i+1][j-1]+M[i][j-1])//6)
    elif i==m-1:
    if j==0:
    r.append((M[i][j]+M[i-1][j]+M[i-1][j+1]+M[i][j+1])//4)
    elif j==n-1:
    r.append((M[i][j]+M[i-1][j]+M[i-1][j-1]+M[i][j-1])//4)
    else:
    r.append((M[i][j]+M[i-1][j]+M[i-1][j+1]+M[i][j+1]+M[i-1][j-1]+M[i][j-1])//6)
    else:
    if j==0:
    r.append((M[i][j]+M[i][j+1]+M[i-1][j]+M[i-1][j+1]+M[i+1][j]+M[i+1][j+1])//6)
    elif j==n-1:
    r.append((M[i][j]+M[i][j-1]+M[i-1][j]+M[i+1][j]+M[i-1][j-1]+M[i+1][j-1])//6)
    else:
    r.append((M[i][j]+M[i][j-1]+M[i-1][j]+M[i+1][j]+M[i-1][j-1]+M[i+1][j-1]+M[i][j+1]+M[i-1][j+1]+M[i+1][j+1])//9)
    ans.append(r)
    r=[]
    return ans

      

[LeetCode&Python] Problem 661. Image Smoother的更多相关文章

  1. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  2. [LeetCode&Python] Problem 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...

  3. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  4. [LeetCode&Python] Problem 371. Sum of Two Integers

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  5. [LeetCode&Python] Problem 520. Detect Capital

    Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...

  6. [LeetCode&Python] Problem 226. Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Tr ...

  7. [LeetCode&Python] Problem 905: Sort Array By Parity

    Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...

  8. [LeetCode&Python] Problem 1: Two Sum

    Problem Description: Given an array of integers, return indices of the two numbers such that they ad ...

  9. [LeetCode&Python] Problem 682. Baseball Game

    You're now a baseball game point recorder. Given a list of strings, each string can be one of the 4 ...

随机推荐

  1. ActiveMQ 集群和主从

    举例说明:假设有 3 个 broker 节点,分别是61616,61618, 61620,其中 61616 和 61618 组成主.从节点,而 61616(或61618)和 61620 构成集群.61 ...

  2. js 手机号码和电话号码正则校验

    checkPhone() { var mobile = ''; var tel = /^0\d{2,3}-?\d{7,8}$/; var phone = /^(((13[0-9]{1})|(15[0- ...

  3. android开发环境搭建教程

    首先安装jdk,然后下载android studio,双击安装即可. 官网:http://www.android-studio.org/ 直接下载链接:https://dl.google.com/dl ...

  4. UNIX-like系统资源检查命令

    系统资源主要是内存.磁盘.CPU三项,其中任一项资源用尽都会造成系统崩溃. 系统 内存 磁盘空间 磁盘io CPU Linux free -g df -h   top AIX svmon -G/vms ...

  5. commonJS 和 ES6 模块化的不同

    commonjs 导出 module.exports={ add:function(){ console.log('add测试') } } 导入 var add=require('./add.js') ...

  6. jackSon注解– @JsonInclude 注解不返回null值字段

    @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; @Js ...

  7. python全栈开发笔记---------变量小结

    变量是什么? 变:变化,重在变字,量:计量,衡量,表示一种状态. 变量字面理解就是一个可能改变的量,也就是这个值是不固定的. 变量名: a.数字 b.字母 c.下划线 变量的定义 level = 1 ...

  8. tomcat 线程数与 mysql 连接数综合调优

    目前线上系统包含 数据收集+数据分析+中心服务,三个均为 tomcat,共用一个mysql服务. 由于tomcat最大线程数200 *3 =600,最大并发时,会有600个jdbc连接.当然这是极端情 ...

  9. 使用gitblit搭建自己的代码存储仓库

    一.基本准备: 服务器:阿里云的windows server 2008 r2 64位 相关软件: 1.java的jdk包: 2.gitblit程序包(这个不用安装,是绿色的). 二.安装步骤 1.下载 ...

  10. Java中的运算符及表达式

    常用的运算符:赋值运算符(=).加法运算符(+).乘法运算符(*).除法运算符(/).括号运算符(( )).余数运算符(%).布尔运算符. 注释符(//).注释的内容为双反斜杠后的内容至换行结束. j ...