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].

题目标签:Array

  题目给了我们一个2d M array,让我们平滑处理图片。对于每一个cell,把它更新为 以自身为中心 3x3 的平均值。

  就用常规方法做,新设一个 res[][] array,遍历M,对于每一个cell, 遍历以它为中心的3x3的cells,得到平均值,存入res。

  需要注意的就是,3x3的边界问题。

Java Solution:

Runtime beats 72.97%

完成日期:10/19/2017

关键词:Array

关键点:处理3x3的边界问题

 class Solution
{
public int[][] imageSmoother(int[][] M)
{
int rows = M.length;
int cols = M[0].length;
int[][] res = new int[rows][cols]; for(int i=0; i<rows; i++)
{
for(int j=0; j<cols; j++)
{
int sum = 0;
int count = 0;
// sum 3x3 area and take care of the boundary
for(int x=Math.max(0,i-1); x<=Math.min(rows-1, i+1); x++)
{
for(int y=Math.max(0, j-1); y<=Math.min(cols-1, j+1); y++)
{
sum += M[x][y]; // sum up cells value
count++; // count cells number
}
} res[i][j] = sum / count; // get average value
}
} return res;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 661. Image Smoother (图像平滑器)的更多相关文章

  1. LeetCode - 661. Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  2. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  3. 【Leetcode_easy】661. Image Smoother

    problem 661. Image Smoother 题意:其实类似于图像处理的均值滤波. solution: 妙处在于使用了一个dirs变量来计算邻域数值,看起来更简洁! class Soluti ...

  4. Java实现 LeetCode 661 图片平滑器(暴力)

    661. 图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元 ...

  5. 【LeetCode】661. Image Smoother 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...

  6. [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 t ...

  7. Leetcode 661.图片平滑器

    图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个 ...

  8. [LeetCode] Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  9. 【LeetCode】1410. 实体解析器 HTML Entity Parser HTML

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 替换 日期 题目地址:https://leetcode ...

随机推荐

  1. JS中有关分支结构、循环结构以及函数应用的一些简单练习

    案例一:搬桌子    年龄大于七岁男女都可以搬桌子,年龄小于七岁大于五岁的男生可以搬桌子: var num =parseInt(prompt("请输入你的年龄")) var sex ...

  2. 从文本中读取内容并把读取到的内容转化成二进制保存的形式(包含十进制数如何转换成二进制数dtob函数)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<malloc.h> d ...

  3. Tiled Editor 图块的两种导入方式

    一.图块集图块的导入. 打开或者创建地图后,新建 新图块. 弹出新图块面板 图块类型选择 "基于图块集图块",一定要选择"嵌入地图",否则需要另存为其他类型的文 ...

  4. java web项目修改favicon.ico图标的方式

    1.修改整个项目的tomcat图标 找到tomcat的根目录(tomcat-webapps-ROOT目录),然后将修改的favicon.ico图标覆盖掉本地的图标,然后再重启项目,刷新,清除浏览器缓存 ...

  5. SQL 常用语法一

    整理笔记,并将常用的SQL语法记录下来. 这些方法有 CASE WHEN, IFNULL,GROUP BY,LIMIT,SUBSTR 1,字段转换 CASE WHEN 意义: If(a==b) a=c ...

  6. js函数基础知识

    [函数的声明及调用] function 函数名(参数1,参数2,....){ //函数体代码 return返回值: } 1.函数的调用: ①直接调用:函数名(参数1的值,参数2的值,....) ②事件 ...

  7. Spring中的Service/DAO/DTO

  8. HCatalog

    HCatalog HCatalog是Hadoop中的表和存储管理层,能够支持用户用不同的工具(Pig.MapReduce)更容易地表格化读写数据. HCatalog从Apache孵化器毕业,并于201 ...

  9. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)ABCD

    A. Arpa and a research in Mexican wave time limit per test 1 second memory limit per test 256 megaby ...

  10. Angular - 预加载 Angular 模块

    Angular - 预加载延迟模块 在使用路由延迟加载中,我们介绍了如何使用模块来拆分应用,在访问到这个模块的时候, Angular 加载这个模块.但这需要一点时间.在用户第一次点击的时候,会有一点延 ...