[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 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:
- The value in the given matrix is in the range of [0, 255].
- The length and width of the given matrix are in the range of [1, 150].
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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, ...
- [LeetCode&Python] Problem 1: Two Sum
Problem Description: Given an array of integers, return indices of the two numbers such that they ad ...
- [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 ...
随机推荐
- WebSphere安装教程(WAS6.1为例)
1.网络准备 我们选择图形界面安装,如果堡垒机是windows则要在目标机器安装桌面环境并开启vcnserver:如果堡垒机是Linux则在堡垒机安装桌面环境和vncserver,然后将目标机的DIS ...
- swiper添加了自动滚动效果,然后用手指划过页面,发现自动滚动效果不生效了
我给swiper添加了自动滚动效果,然后用手指划过页面,发现自动滚动效果不生效了,哪里出了问题呢? 添加参数 autoplayDisableOnInteraction : false,
- JQuery对象和DOM对象的区别与转换
刚开始学习JQuery,经常分不清楚哪些是JQuery对象,哪些是DOM对象,了解它们之间的关系是很有必要的. 1.DOM对象和JQuery对象的区别 1) DOM对象 DOM是Document O ...
- @Configuration的使用 和作用
1从spring4.0以后,@Spring boot Application就包含了@ComponentScan,@ComponentScan就不用写了 2@MapperScan(basePackag ...
- mac+php+nginx+laravel配置启动
首先保证mac安装php,nginx,composer 根据laravel中文文档进行安装 http://laravelacademy.org/post/6665.html 直接指向 composer ...
- Win10系列:C#应用控件基础2
HyperlinkButton控件 HyperlinkButton控件是以超链接文本形式显示的按钮,可以为其NavigateUri属性设置一个URI地址,当单击超链接文本按钮时,将会使用浏览器打开在H ...
- 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 ...
- bzoj4278
题解: 把第一个串放好,加一个oo 然后把第二个串倒序放进来 然后就是http://www.cnblogs.com/xuanyiming/p/8510231.html这一题了 代码: #include ...
- C++输出数组名
1.如果C++输出的数组是char类型的,那么输出的就是数组中的元素. 2.如果使用的是其他类型的数组作为输出的话,那么就是一个16进制的地址. 3.还是那句话,对数组的操作,很多时候都是指针的操作, ...
- 以黄门镇黄湾村某一扶贫文档为例——将Excel数据填入到已存在的Word模板
傻瓜可以写出机器读得懂代码,但写出让人能读懂的代码的是优秀程序员 作用:通过Excel文件中的一列数据作为文件名创建Word文档,并将Excel中的一行数据填一表,实现自动化 Excel的VBA宏代码 ...