[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 ...
随机推荐
- WannaCry(永恒之蓝)病毒处理方法
1.直接关闭server服务 打开cmd执行关闭server服务即可: net stop server 控制面板--管理工具--服务里手动关掉 2.防火墙限制445端口 3.打补丁 [KB401259 ...
- Pinpoint是一个开源的 APM (Application Performance Management/应用性能管理)工具,用于基于java的大规模分布式系统,基于Google Dapper论文
Pinpoint是一个开源的 APM (Application Performance Management/应用性能管理)工具,用于基于java的大规模分布式系统,基于Google Dapper论文 ...
- Java文档注释导出帮助文档和项目的jar包导入和导出。
1.1 文档注释导出帮助文档 在eclipse使用时,可以配合文档注释,导出对类的说明文档,从而供其他人阅读学习与使用. 通过使用文档注释,将类或者方法进行注释用@简单标注基本信息.如@au ...
- zabbix3.4.7配置邮件告警详细步骤
Zabbix服务器操作 1. 安装sendmail或postfix (邮件传送代理MTA),本教程使用sendmail软件. (标注:如果直接使用外部邮箱发送邮件可以不需要配置sendmail或po ...
- 用linux命令连接无线网络-转载
首先是用到的工具: ifconfigrouteiwlistiwconfig 后两个是无线工具 从现在开始,按我的步骤做 (##后面的是说明部分) 1.开启无线,如果是笔记本,开启无线开关,或用Fn+F ...
- TCP_NODELAY算法使用事项
当有一个TCP数据段不足MSS,比如要发送700Byte数据,MSS为1460Byte的情况.nagle算法会延迟这个数据段的发送,等待,直到有足够的数据填充成一个完整数据段.也许有人会问,这有什么影 ...
- TiDB 深度实践之旅--真实“踩坑”经历
美团点评 TiDB 深度实践之旅(9000 字长文 / 真实“踩坑”经历) 4 PingCAP · 154 天前 · 3956 次点击 这是一个创建于 154 天前的主题,其中的信息可能已经有所发 ...
- 锤子科技 Smartisan M1L 咖啡金 真皮背面 高配版 5.7
http://www.smartisan.com/m1/#/os 快人一步的OS http://www.smartisan.com/shop/#/buyphone?c=coffee&v= ...
- flask 开启多线程
app.run(debug=True, threaded=True)
- sqlalchemy动态组合查询语句。
if filter_type == 1: search = and_(GameRoom.status ==1,or_( and_(GameRoom.white_user_id == user_id, ...