[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 ...
随机推荐
- python3+ftplib实现ftp客户端
一.程序说明 1.1 程序实现关键点 python实现ftp客户端,主要会遇到以下四个问题: 第一个问题是使用什么包实现----我们这里是使用标准库中的ftplib 第二个问题是怎么连接登录ftp服务 ...
- net core 模型绑定与之前版本的不同-FromBody 必须对应Json格式
之前有一个用于七牛上传图片的Callback Url的WebAPI (之前是用.net4.0,运行正常) 代码如下: // 七牛CallBack地址,CallbackBody内容name=upload ...
- cin.get()函数使用例子
#include <iostream>using namespace std; int k = 0; int main(){ char a[1000]; char c; do { cin. ...
- dl简单模板,无pretraining过程
layer_dimensions = [11 22 33 22 11]'; ld_size = size(layer_dimensions , 1); % what is deal [x rx dx ...
- Jenkins结合testng注意事项
1.在生成测试报告时,因为Jenkins自带的只有Junit的测试报告,不会显示testng的. 2.要想显示Publish TestNG Results这一项,首先需要在jenkins的首页-系统管 ...
- laravel框架5.2版本组件包开发
一.包的作用 1 把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2 如同文件夹一样,包也采用了树形目录的存储方式.同一个包中的类名字是不同的,不同的包中的类的名字是可以相同的, ...
- python,判断操作系统是windows,linux
import sys,platform print(sys.platform) print(platform.system()) sys.platform: 获取当前系统平台. platform.sy ...
- java有关构造器的面试题详解
1,编译器只会提供自动提供一个默认的无参数的构造函数 2,如果程序员没有给类A没有提供构造函数,则编译器会自动提供一个默认的无参数的构造函数,如果用户提供了自己的构造函数,则编译器就不在提供默认的无参 ...
- dedecms中调用隐藏栏目的方法
第一种情况用SQL标签如下: {dede:sql sql='Select * from dme_arctype where ishidden=1 and topid=2'} <span clas ...
- hustoj 管理员和后台设置
之前用过hustoj 的livecd版本,觉得有一些小问题,所以从头到尾搭建.主要包含的过程包括: 安装ubuntu系统 搭建hustoj 管理员和后台资源建设 本文介绍如何在搭建好hustoj的基础 ...