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
class Solution {
public int[][] imageSmoother(int[][] m) {
if (m == null)
return null;
int[][] ret = new int[m.length][m[0].length];
for (int i=0; i<m.length; i++) {
for (int j=0; j<m[i].length; j++) {
int sum = m[i][j], cnt = 1;
if (i-1 >= 0) {
sum += m[i-1][j];cnt++;
if (j-1 >= 0) {
sum += m[i-1][j-1];
cnt ++;
}
if (j+1 < m[i].length) {
sum += m[i-1][j+1];
cnt ++;
}
}
if (i+1 < m.length) {
sum += m[i+1][j];cnt++;
if (j-1 >= 0) {
sum += m[i+1][j-1];
cnt++;
}
if (j+1 < m[i].length) {
sum += m[i+1][j+1];
cnt++;
}
}
if (j-1 >= 0) {
sum += m[i][j-1];
cnt++;
}
if (j+1 < m[i].length) {
sum += m[i][j+1];
cnt++;
}
ret[i][j] = sum / cnt;
}
}
return ret;
}
}

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. 【LeetCode】661. Image Smoother 解题报告(Python)

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

  5. [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 ...

  6. [LeetCode] 661. Image Smoother_Easy

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

  7. 661. Image Smoother色阶中和器

    [抄题]: Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...

  8. 661. Image Smoother@python

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

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

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

随机推荐

  1. SpringBoot介绍及环境搭建

    什么是SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不 ...

  2. 项目启动log4j相关警告问题

    在项目启动时出现了下面的警告: log4j:WARN custom level class [xxxxx] not found. 出现这个错误,不是log4j的问题,也是slf4j的问题,问题是因为自 ...

  3. hbase伪分布式安装(单节点安装)

    hbase伪分布式安装(单节点安装) http://hbase.apache.org/book.html#quickstart   1.    前提配置好java,环境java变量     上传jdk ...

  4. Tomcat软件使用常见问题

    Tomcat软件使用常见问题 tomcat软件使用的常见问题 1)闪退问题 原因:tomcat软件是java语言开发的. tomcat软件启动时,会默认到系统的环境变量中查找一个名称叫JAVA_HOM ...

  5. final、finally、finalize

    final是一个修饰词.可以修饰变量.方法.类 final修饰变量时分为两种 )1.修饰成员变量:该成员变量不可以被二次赋值.也就是说成员变量无法改变.且该成员变量要么在定义时初始化,要么在构造器中进 ...

  6. 如何查看dede版本信息

    dedecms版本信息 更新日期 it 分类: dedecms 打开 /include/common.inc.php 查找 $cfg_version 可以看到版本号 /打开 data/admin/ve ...

  7. LNMP环境的搭建

    http://blog.csdn.net/wzy_1988/article/details/8438355#

  8. jQuery——动态给表格添加序号

    摘录自:http://www.cnblogs.com/picaso/archive/2012/10/08/2715564.html 很多时候遇到需要对表格动态操作,而且一般都会有表格的序号,但是有时候 ...

  9. list类型

    list是一个链表结构,可以模拟栈,队列 lpush list中压入一个元素   模拟栈 lrange  取出list的中的元素(0 -1 表示从头取到尾) rpush list中压入一个元素   模 ...

  10. Linux实践篇--crontab定时任务

    原文出处:http://www.cnblogs.com/tracy/archive/2011/12/27/2303788.html.感谢作者的无私分享 一.  Crontab 介绍           ...