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. npm使用国内淘宝镜像的方法

    一.通过命令配置 1. 命令 npm config set registry https://registry.npm.taobao.org 2. 验证命令 npm config get regist ...

  2. 小程序util工具

    import wafer from '../lib/wafer2-client-sdk' import tip from './tip' // 时间格式化 const formatTime = tim ...

  3. java生成word的几种方案

    http://blog.sina.com.cn/s/blog_a5e968370101crtl.html 1. Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建 ...

  4. IntelliJ Idea 快捷键列表

    最重要:1Ctrl+Alt+Shift+T:查找类2重构3提取父类 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift ...

  5. NumPy for MATLAB users

    http://mathesaurus.sourceforge.net/matlab-numpy.html Help MATLAB/Octave Python Description dochelp - ...

  6. zabbix3.4.7实现企业微信告警,亲测可用!

    Zabbix 新版微信告警 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信 ...

  7. windows 路由的配置

    查看ip路由表 route print : netstat -r windows 下添加一条路由 route命令 route [-f][-p][command [distinataion] [MASK ...

  8. js数组及常用数学方法

    数组方法 清空数组   1: arr.length=0;   2: arr=[]; arr.push()          //往数组最后一个添加元素,会待会一个返回值,就是新的数组长度arr.uns ...

  9. Django+Xadmin+Echarts动态获取数据legend颜色显示灰色问题已解决

    前段时间做的使用Django的Xadmin后台和百度Echarts进行后台数据可视化,功能虽然实现,展示出来的legend图例,都是灰色的,只有鼠标放上去才会显示彩色的.百度都快被我刨穿了,看到有类似 ...

  10. Thêm Một Lần Đau--错错错--IPA--越南语

    越南国际天团HKT的名曲.