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. Java Web(九) JDBC及数据库连接池及DBCP,c3p0,dbutils的使用

    DBCP.C3P0.DBUtils的jar包和配置文件(百度云盘):点我下载 JDBC JDBC(Java 数据库连接,Java Database Connectify)是标准的Java访问数据库的A ...

  2. 【Java算法】获得一个随机字符串

    package suanfa; import java.util.Random; public class RandomStr { public static String getRandomStr( ...

  3. X的平方

    题目描述: 实现 int sqrt(int x) 函数,计算并返回 x 的平方根. 样例 sqrt(3) = 1 sqrt(4) = 2 sqrt(5) = 2 sqrt(10) = 3 题解: 解法 ...

  4. Win10系列:UWP界面布局进阶7

    Canvas Canvas元素用于定义一个区域,可以向这个区域中添加不同的XAML界面元素.Canvas会对其内部的元素采用绝对布局方式进行布局,下面通过三个示例来介绍Canvas的使用方法. (1) ...

  5. PHP开发者的Linux学习之路

    谈起一个高效动态网站的构建,那就不得不提到LAMP,即Linux操作系统.Apache网络服务器.Mysql数据库.Perl.PHP或Python编程语言等开源产品所组成的网站架构框架,其最大的优势是 ...

  6. unity中实现三个Logo图片进行若隐若现的切换并有延时切换图片的效果

    public GameObject canvas; private Transform logoParent; private Transform Logo_logo; //logo一 private ...

  7. jdk重装后com.sun.tools.javac.Main is not on the classpath的问题 .

    在重装了JDk之后,在编译工程的时候出现如下错误: com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME does no ...

  8. jenkins部署web项目到tomcat(五)

    (1)maven构建web项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  9. 【转】Mac OS X Terminal 101:终端使用初级教程

    最近学习苹果认证的<Mac OS X Support Essentials>教程,看到 Command Line 一节有很多实用的知识,下面选取一部分翻译 + 笔记,整理成此文. 你可以整 ...

  10. 准备下上机考试,各种排序!!以后再添加和仿真像wiki上那样!

    #include <stdio.h> #include <string.h> #define N 6 typedef struct { ]; int score; }stude ...