[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 ...
随机推荐
- Windows添加用户和组命令
查看当前存在用户: net user 查看当前用户组: net localgroup 添加用户(以添加用户ls密码abcd1234为例): net user ls abcd1234 /add 添加用户 ...
- WebSphere安装教程(WAS6.1为例)
1.网络准备 我们选择图形界面安装,如果堡垒机是windows则要在目标机器安装桌面环境并开启vcnserver:如果堡垒机是Linux则在堡垒机安装桌面环境和vncserver,然后将目标机的DIS ...
- 小程序使用wxParse解析html
转载至:http://blog.csdn.net/tang05709/article/details/54924675 这个是需要循环出显示的https://github.com/tonyzhan/c ...
- MSOCache office问题
\MSOCache 链接:https://pan.baidu.com/s/1dVvjYnD0D6DG6oFK_Mww2w密码:qrvp undefied
- sql server 根据身份证号计算出生日期和年龄的存储过程
我这边有一个业务,需要客户填写身份证号,自动计算他的出生日期和年龄 在sql中,具体的存储过程实现是这样的: /******************************************** ...
- error: http://ppa.launchpad.net lucid Release: The following signatures couldn't be verified because
ubuntu 命令行sudo apt-get update W: GPG error: http://ppa.launchpad.net lucid Release: The following si ...
- window7下载安装桌面版ubuntu
首先需要下载VMware Workstation 下载地址:http://pan.baidu.com/s/1qXS0rhi 秘钥:bbpn 我的环境是ubuntu-14.10-desktop-a ...
- AI工具5.13
如果想选中上面的很多图形,可以锁定不需要选择的下面的图形.选择需要锁定的对象.“对象”“锁定”“所选对象” “对象”“变换”“再次变换”快捷键“ctrl=d"一般前面有其他操作如“移动”“复 ...
- AttributeError: 'cx_Oracle.Cursor' object has no attribute 'numbersAsStrings'
转载自:https://www.wengbi.com/thread_77579_1.html 最近在本地搭建Django开发环境,Django 1.11,python 2.7.11,数据库Oracle ...
- Linux Shell 编程 教程 常用命令
概述: Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提供了一个界面,用户 ...