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

 1 class Solution {
 2     public int[][] imageSmoother(int[][] M) {
 3         if(M == null || M.length == 0 || M[0].length == 0){
 4             return M;
 5         }
 6
 7         int m = M.length;
 8         int n = M[0].length;
 9         int [][] res = new int[m][n];
10
11         for(int i = 0; i<m; i++){
12             for(int j = 0; j<n; j++){
13                 int count = 0;
14                 int sum = 0;
15                 for(int iInc : new int[]{-1,0,1}){
16                     for(int jInc : new int[]{-1,0,1}){
17                         if(isValid(i+iInc, j+jInc, m, n)){
18                             count++;
19                             sum += M[i+iInc][j+jInc];
20                         }
21                     }
22                 }
23                 res[i][j] = sum/count;
24             }
25         }
26         return res;
27     }
28
29     private boolean isValid(int i, int j, int m, int n){
30         return i>=0 && i<m && j>=0 && j<n;
31     }
32 }

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7524276.html

LeetCode Image Smoother相关推荐

  1. leetcode (Image Smoother)

    Title:Image Smoother    661 Difficulty:Easy 原题leetcode地址:https://leetcode.com/problems/image-smoothe ...

  2. C#LeetCode刷题之#661-图片平滑器( Image Smoother)

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

  3. LeetCode 661. Image Smoother

     题目描述 Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...

  4. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  5. LeetCode 简单算法题

    使用Nodejs 抓取的LeetCode 简单算法题  一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...

  6. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  7. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  8. Leetcode题目练习总结(持续更新......)

    Leetcode题目练习 数组 1.两数之和 26. 删除排序数组中的重复项 27. 移除元素 35.搜索插入位置 53.最大子序列 66.加一 88.合并两个有序数组 118.杨辉三角 119.杨辉 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. 深入理解Linux I/O系统
  2. 简单的用Python实现学生信息管理
  3. 【期望】乘坐电梯(金牌导航 期望-2)
  4. File was loaded in the wrong encoding: 'UTF-8'
  5. springboot 没有找到service_Spring Boot 应用程序五种部署方式
  6. 解析Excel_Poi
  7. HTML ===> 页面添加数学公式
  8. 二、列表(java)
  9. 完全数(Perfect Number)
  10. 公司职位英文及缩写大全
  11. 用Python编写账号密码登录程序
  12. 获取高匿代理ip的步骤思路(推荐使用--刚找到的方法判断是否高匿)
  13. Windows 10 控制面板 (Control Panel)
  14. springnative让java应用脱离jvm
  15. python画美国队长盾牌——turtle库
  16. 设计分享 | 基于51单片机实现温度监测报警系统
  17. 学习下什么是微服务架构/平台
  18. 工具条(Ext.Toolbar)
  19. Verdi的简单使用命令笔记
  20. C++ reverse()函数

热门文章

  1. 东京奥组委公布奥运志愿者和城市志愿者昵称
  2. HC-SR505人体红外报警系统
  3. 关闭WIN7交互式服务检测提示
  4. VS 2019教程:创建ASP.NET Core Web App
  5. 哪款立体声骨传导蓝牙耳机好,推荐几款目前主流的骨传导耳机
  6. 要不做一名 Prompt Engineer
  7. Scala 继承和特质
  8. 腾讯2017秋招笔试编程题--游戏任务标记
  9. 51nod1463 找朋友
  10. ListView--QQ联系人样式