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. EF+MVC学习中的不理解的问题

    1.之所以被定义为virtual便于实现延迟加载 代码: public virtual ICollection<Enrollment> Enrollments { get; set; } ...

  2. gettype

    取得变量的类型. 语法: string gettype(mixed var); 返回值: 字符串 函数种类: PHP 系统功能 内容说明 本函数用来取得变量的类型.返回的类型字符串可能为下列字符串其中 ...

  3. 获取select中的值

    分别使用javascript原生的方法和jquery方法<select id="test" name=""> <option value=&q ...

  4. 【开发技术】Xcode3与xcode4.2模板对比(Xcode4.2开发之一些变化)

    Xcode3中IOS下的Application的模板如下: Navigation_Based Application OpenGL ES Application Tab Bar Application ...

  5. linux pagecache限制与查看

    在linux服务器使用过程中,由于linux对内存的使用原则是能cache就尽量cache,所以会出现pagecache占用很多的情况. suse的版本有一个pagecachelimit的功能,cen ...

  6. 搞个小项目吧,做一个ppt播放器

    先来两个参考链接,接下来再进行实战 http://www.geek-workshop.com/forum.php?mod=viewthread&tid=1137 http://www.geek ...

  7. js 原型 函数和对象的关系

    函数就是对象的一种  instanceof  可以做判断 var fn = function(){}; fn instanceof Object //true Object构造函数的prototype ...

  8. crypto在web的使用

    前言 crypto 在nodejs中是一个核心模块,虽然现在高等浏览器中也有了crypto全局对象(下图),它在nodejs中的使用与web端还是不同的. web端使用cryptojs 国外下载较慢, ...

  9. getRequestDispatcher()和response.sendRedirect()

    request.getRequestDispatcher()是请求转发,前后页面共享一个request   response.sendRedirect()是重新定向,前后页面不是一个request.

  10. android onSaveInstanceState应用实例

    //activity销毁之前调用,把状态值存储上 @Override protected void onSaveInstanceState(Bundle outState) { outState.pu ...