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. notepad++自动对齐使用空格代替Tab并将空格显示为小点

    一.说明 对大多数语言而言自动对齐使用空格还是tab对编译运行并没有什么影响,但对python问题就很大:因为就算是缩进看起来是一样的但某些行用空格某些行用tab运行会报错. 另外除了空格替换tab外 ...

  2. AIX安装JDK1.7教程

    1.下载 下载链接(下载需要登录IBM账号):https://www.ibm.com/developerworks/java/jdk/aix/service.html#i1 由于jdk版本AIX操作系 ...

  3. rdesktop安装教程

    1.介绍 rdesktop是Linux下连接windows远程桌面的工具 2.下载 https://github.com/rdesktop/rdesktop/releases 3.安装 mkdir / ...

  4. 微信小程序 无限加载 上拉加载更多

    加载更多,其实就是再次向接口发送请求,把返回的数据,追加到渲染页面的数组里的过程,具体实现实例如下: demo.js // pages/project/project.js const app = g ...

  5. shiro学习笔记-Subject#login(token)源码实现过程

    追踪Subject的login(AuthenticationToken token)方法,其调用的为DelegatingSubject类的login方法,DelegatingSubject实现了Sub ...

  6. react之传递数据的几种方式props传值、路由传值、状态提升、redux、context

    react之传递数据的几种方式 1.父子传值 父传值:<子的标签 value={'aaa'} index={'bbb'}></子的标签> 子接值:<li key={thi ...

  7. 1-3Controller之Response

    控制器中的方法: public function response1(){ /*响应的常见类型: * 1.字符串 * 2.视图 * 3.json * 4.重定向 * */ //响应JSON /*$da ...

  8. 替代iframe

    1.jq中 通过JQuery的load()方法动态加载页面. $( "#result" ).load( "app/test.html" ); 2.vue.rea ...

  9. 安装Adobe Acrobat XI Pro

    从网上下载Adobe Acrobat XI Pro这款软件,下载后将其解压到我们的电脑上,然后找到setup.exe双击安装它,安装时选择“使用试用版本或订阅” 2 选择“自定义”   自定义安装组件 ...

  10. Oracle 12c新特性

    转载自:Oracle 12c新特性(For DBA) 一: Multitenant Architecture (12.1.0.1)      多租户架构是Oracle 12c(12.1)的新增重磅特性 ...