[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 ...
随机推荐
- openssl修改版本号
1.查看当前openssl版本号 openssl version 2.查看openssl所在位置 which openssl 3.查看保存版本号的libcrypto.so所在位置 ldd /usr/b ...
- C++简单输入输出-计算火车运行时间
//写的很差,无力tc 7-4 计算火车运行时间 (17 分) 本题要求根据火车的出发时间和达到时间,编写程序计算整个旅途所用的时间. 输入格式: 输入在一行中给出2个4位正整数,其间以空格分隔,分别 ...
- Win10系列:JavaScript综合实例1
上面几个小节讲解了使用HTML5和JavaScript语言开发Windows 应用商店应用时会用到的一些技术,本小节将前面介绍的知识融合在一起创建一个菜谱应用程序,帮助读者更进一步地理解和掌握这些知识 ...
- day03 基本数据类型
1.什么是数据类型 变量值即我们 存放的数据 ,数据类型及变量值的类型 2.变量值为何要区分类型 因为变量值使用记录现实世界中事物的特征,针对不同的特征就应该用不同类型的值去标识 3.如何应用数据类型 ...
- bzoj1010
题解: 斜率优化dp f[i]=f[j]+(i-j+sum[i]-sum[j]-L)^2 然后斜率优化 代码: #include<bits/stdc++.h> typedef long l ...
- 《Python》进程收尾线程初识
一.数据共享 from multiprocessing import Manager 把所有实现了数据共享的比较便捷的类都重新又封装了一遍,并且在原有的multiprocessing基础上增加了新的机 ...
- SmtpClient SSL 发送邮件异常排查
上周使用 SmtpCliet 发送邮件测试,在服务端配置 SSL 465 / 993 情况 ,客户端使用 465 SSL 端口发送邮件异常,测试代码如下: System.Net.ServicePoin ...
- Android Touch事件之二:dispatchTouchEvent()和onTouchEvent()篇
2015-12-01 15:06:14 Android Touch事件第一篇:Touch事件在父ViewGroup和子View之间的传递简单分析了事件的传递流程,这次深入了解下dispatchTouc ...
- SQL-26 (二次分组)汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
题目描述 汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no.dept_name.其当前员工所有的title以及该类型title对应的数目countCREATE TABLE ...
- 驱动链表(LIST_ENTRY)
DDK提供了两种链表的数据结构,双向链表和单向链表,其定义如下: typedef struct _LIST_ENTRY { struct _LIST_ENTRY *Flink; struct _LIS ...