作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/image-smoother/description/

题目描述

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:

  1. The value in the given matrix is in the range of [0, 255].
  2. The length and width of the given matrix are in the range of [1, 150].

题目大意

对一张图片取模糊,每个位置的数值等于9联通区域内的所有数字的平均值取整。

解题方法

方法一:暴力解决

非常粗暴的解法。判断数组中每个元素的周边的所有元素的和,并记录元素的个数。用判断边界的方式,防止过界。

from copy import deepcopy as copy
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
if not M or not M[0]:
return M
rows = len(M)
cols = len(M[0])
isValid = lambda i,j: i >=0 and i < rows and j >= 0 and j < cols
row, col = 0, 0
answer = copy(M)
for row in xrange(rows):
for col in xrange(cols):
_sum, count = 0, 0
for i in xrange(-1, 2):
for j in xrange(-1, 2):
if isValid(row + i, col + j):
_sum += M[row + i][col + j]
count += 1
answer[row][col] = _sum / count
return answer

日期

2018 年 1 月 24 日
2018 年 11 月 16 日 —— 又到周五了!

【LeetCode】661. Image Smoother 解题报告(Python)的更多相关文章

  1. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. R语言与医学统计图形【6】低级绘图函数

    R语言基础绘图系统 基础绘图包之低级绘图函数--定义坐标轴.图例.文本 低级绘图函数:本身不具备图形绘制能力,只是在已有图形基础上添加元素. 函数 功能 arrows 添加箭头 axis 坐标轴 bo ...

  2. 以VuePress的v1.x为基础开发-用户手册

    首先配置.vuepress中的config.js module.exports = { title:"用户手册", description: '用户手册', evergreen: ...

  3. YARP+AgileConfig 5分钟实现一个支持配置热更新的代理网关

    YARP 是微软开源的一个反向代理项目,英文名叫 Yet Another Reverse Proxy .所谓反向代理最有名的那就是 nginx 了,没错 YARP 也可以用来完成 nginx 的大部分 ...

  4. IT四大名著

    标题耸人听闻,sorry. CPU.操作系统.编译器和数据库我都不会.我英语也不行,但我认识所有的字母.:-) 万一有人感兴趣呢?https://sqlite.org/doclist.htmlThe ...

  5. Oracle中分割逗号函数REGEXP_SUBSTR

    最近优化FORM中的查询条件遇到某个字段可以选取多个值的问题,思路当然就是选取时将多个值通过某个符号拼接起来,查询数据的时候将拼接后的字符串按照符号分割开,在分割逗号的时候用到了一个新的方法REGEX ...

  6. Java 8实现BASE64编解码

    Java一直缺少BASE64编码 API,以至于通常在项目开发中会选用第三方的API实现.但是,Java 8实现了BASE64编解码API,它包含到java.util包.下面我会对Java 8的BAS ...

  7. Give You My Best Wishes

    亲耐滴IT童鞋们: 感谢大家一直以来的支持,因为有你们的支持,才有我这么"拼"的动力!!爱你们哟 OC的学习已经告一段落,希望大家通过阅读这几篇浅薄的随笔,能够寻找到解决问题的方法 ...

  8. OC简单介绍

    一.OC与C的对比 关键字 OC新增的关键字在使用时,注意部分关键字以"@"开头 方法->函数 定义与实现 数据类型 新增:BOOL/NSObject/id/SEL/bloc ...

  9. 【Linux】【Shell】【Basic】文件查找locate,find

    1.locate:   1.1. 简介:依赖于事先构建好的索引库: 系统自动实现(周期性任务): 手动更新数据库(updatedb):               1.2. 工作特性:查找速度快:模糊 ...

  10. sqlserver 各种判断是否存在(表、视图、函数、存储过程等)

    1.判断表是否存在 select * from sysobjects where id = object_id(表名) and OBJECTPROPERTY(id, N'IsUserTable') = ...