原题链接在这里: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].

题解:

看周围8个点加自己是否是boundary内,若在就更新sum, count++. 最后计算floor average.

Time Complexity: O(m*n), m = M.length, n = M[0].length.

Space: O(m*n), res size.

AC Java:

 class Solution {
public int[][] imageSmoother(int[][] M) {
if(M == null || M.length == 0 || M[0].length == 0){
return M;
} int m = M.length;
int n = M[0].length;
int [][] res = new int[m][n]; for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
int count = 0;
int sum = 0;
for(int iInc : new int[]{-1,0,1}){
for(int jInc : new int[]{-1,0,1}){
if(isValid(i+iInc, j+jInc, m, n)){
count++;
sum += M[i+iInc][j+jInc];
}
}
}
res[i][j] = sum/count;
}
}
return res;
} private boolean isValid(int i, int j, int m, int n){
return i>=0 && i<m && j>=0 && j<n;
}
}

LeetCode Image Smoother的更多相关文章

  1. [LeetCode] Image Smoother 图片平滑器

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

  2. LeetCode 661. Image Smoother (图像平滑器)

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

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

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

  4. LeetCode - 661. Image Smoother

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

  5. LeetCode算法题-Image Smoother(Java实现)

    这是悦乐书的第282次更新,第299篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第150题(顺位题号是661).给定表示图像灰度的2D整数矩阵M,您需要设计一个平滑器以 ...

  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. C#LeetCode刷题之#661-图片平滑器( Image Smoother)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3730 访问. 包含整数的二维矩阵 M 表示一个图片的灰度.你需要 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. [Swift]LeetCode661. 图片平滑器 | Image Smoother

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

随机推荐

  1. iOS 绘制漂亮的折线图

    效果图是这样的: 关键是在 基本策略 是描点 和 连线. 折线基础版本,我找到网上的第三方,UUChart . 特点是朴素,简单,扩展性强. 效果图重点是在折线要有阴影,于是 重点 放在 折线发散的阴 ...

  2. 每天一个Linux命令(47)route命令

        Linux系统的route命令用于显示和操作内核IP路由表(show / manipulate the IP routing table).     (1)用法:     用法:  route ...

  3. 每天一个Linux命令(45)lsof命令

        lsof命令用于查看你进程打开的文件,端口(TCP.UDP),找回/恢复删除的文件,打开文件的进程.     (1)用法:     用法:  lsof  [参数]  [文件]     (2)功 ...

  4. 【Head First Servlets and JSP】笔记9:属性的作用域、线程安全

    什么是属性? 属性和参数 属性的3个作用域 属性API 属性不好的一面 1.到底什么是属性(Attribute)? 属性就是一个对象,可以被设置(bound,也可以叫绑定)到另外三个servlet A ...

  5. 关于pycharm中的requirements.txt文件

    作用:记录所有所依赖的第三方模块,方便迁移到不同的环境中后,防止缺少模块,或因为所依赖的第三方模块版本不同引起不必要的问题 生成命令:pip freeze > requirements.txt ...

  6. python去掉行尾的换行符

    python去掉行尾的换行符 mystring.strip().replace(' ', '').replace('\n', '').replace('\t', '').replace('\r', ' ...

  7. mac下安装py第三方库到python3下

    python3 -m pip install **** 中间可能碰到超时问题 python3 pip --default-timeout=100 install -U **** 设置默认超时时间即可 ...

  8. ZooKeeper服务-数据模型

    ZooKeeper是一个具有高可用性的高性能协调服务. 数据模型 ZooKeeper维护着一个树形层次结构,树中的节点被称为znode.Znode可以用于存储数据,并且有一个与之相关联的ACL(Acc ...

  9. QT 创建对话框 Dialog 实例

    1. 2. dialog.h 头文件 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> QT_BEGIN_NAMESPACE cla ...

  10. BZOJ3244/UOJ122 [Noi2013]树的计数

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...