[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 ... 
随机推荐
- Redis在CentOS7中的启动警告
			CentOS7安装Redis,启动时会出现如下图3个警告. 问题1:WARNING: The TCP backlog setting of 511 cannot be enforced because ... 
- angular组件之间的通讯
			组件通讯,意在不同的指令和组件之间共享信息.如何在两个多个组件之间共享信息呢. 最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有.....我也找找了很多关于组件之间通讯的方法,不同的 ... 
- koa学习
			http://www.ruanyifeng.com/blog/2017/08/koa.html 
- linux下top命令参数详解
			top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. 内存信息.内容如下: Mem: 191272k to ... 
- laravel中的plicy授权方法:
			1.用命令新建policy: php artisan make:policy PostPolicy 2.在app/Policies/PostPolicy.php中添加处理文件的权限的方法: //修改: ... 
- laravel使用使用 Php Artisan Tinker 实现模型的增删改查
			tinker命令: php artisan tinker 查阅数据库数据: App\User::count(); App\User::where('username', 'samuel')->f ... 
- Spring Boot + Spring Cloud 实现权限管理系统 (集成 Shiro 框架)
			Apache Shiro 优势特点 它是一个功能强大.灵活的,优秀开源的安全框架. 它可以处理身份验证.授权.企业会话管理和加密. 它易于使用和理解,相比Spring Security入门门槛低. 主 ... 
- OO第二次课程总结分析
			前几次的作业都是单线程的,总体来说和以前的思维模式和调试等存在着一定的挂钩,在设计上整体难度还不算太大,这次开始了多线程编程,难度可以说是质的飞跃,构思上所考虑的不止一点两点,在整体的基础上还要考虑线 ... 
- svn服务器搭建及使用(一)
			这里郑重感谢分享作者的辛苦:http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html Subversion是优秀的版本控制工 ... 
- Centos7安装RabbitMQ解决Erlang依赖报错
			通过yum等软件仓库都可以直接安装RabbitMQ,但版本一般都较为保守. RabbitMQ官网提供了新版的rpm包(http://www.rabbitmq.com/download.html),但是 ... 
