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. UE4 小笔记

    1,设置postprocess材质时使用Add or Update Blendable 用Make PostprocessSetting会报警告,因为C++代码中没有设置为BlueprintReadW ...

  2. PHP开发api接口安全验证

    php的api接口 在实际工作中,使用PHP写api接口是经常做的,PHP写好接口后,前台就可以通过链接获取接口提供的数据,而返回的数据一般分为两种情况,xml和json,在这个过程中,服务器并不知道 ...

  3. Linux chmod命令及权限含义

    对于一条权限赋值命令:sudo chmod  -R 764 ×××:我们必须了解其含义: (1)各字段含义:sudo chmod  -R(更改文件夹及其子文件夹)                    ...

  4. 为了解决linux配置Nginx 只能关闭防火墙才能访问的问题

    使用Nginx和iptables做访问权限控制(IP和MAC)     之前配置的服务器,相当于对整个内网都是公开的,而且,除了可以通过80端口的nginx来间接访问各项服务,也可以绕过nginx,直 ...

  5. 【开发技术】常用正则表达式大全!(例如:匹配中文、匹配html)

    匹配中文字符的正则表达式: [u4e00-u9fa5]   评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^x00-xff] 评注:可以用来计算字符串的长度 ...

  6. 一简单的RPC实例(Java)

    来至于阿里liangf:如有冒犯,请原谅 RPCFrameWork: package com.sunchao.demo; import java.io.IOException; import java ...

  7. Java中实例方法、类方法和构造方法

    类方法,有static修饰符,典型的主函数public static void main(String[] args){}实例方法,就是一般的方法构造方法,没有返回值(就是连void都没有),方法名与 ...

  8. Android开发模板代码(一)——简单打开图库选择照片

    首先,先贴上样本代码 //检查权限 public void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manife ...

  9. html中的title和alt

    alt是html标签的属性,而title既是html标签,又是html属性. title标签这个不用多说,网页的标题就是写在<title></title>这对标签之内的.tit ...

  10. 我的运维之旅-查找文本的linux命令

    小伙伴们肯定都遇到这么尴尬场景,线上服务出问题了,老大一直在问什么问题导致的,而你由于对查找文本的命令不太熟,鼓捣了半天才找到那条 异常日志,而这时可能半个小时都已经过去了.老大可能对你失望透顶了.讲 ...