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

  1. Input:
  2. [[1,1,1],
  3. [1,0,1],
  4. [1,1,1]]
  5. Output:
  6. [[0, 0, 0],
  7. [0, 0, 0],
  8. [0, 0, 0]]
  9. Explanation:
  10. For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
  11. For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
  12. 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. }

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. $ python正则表达式系列(1)——正则元字符

    本文主要介绍python中正则表达式的基本用法,做一个初步的认识. 1. 初识 python通过re内置模块来处理正则表达式(regex),底层使用C引擎.一个简单的正则匹配的例子: import r ...

  2. Linux基本命令 压缩命令

    1.压缩命令 ================================================================================== 命令名称:gzip ...

  3. java实现文件的压缩和解压

    java实现文件的压缩和解压 代码压缩实现 package com.hjh.demo.zip; import java.io.BufferedInputStream; import java.io.F ...

  4. problem-1003(恢复一下)

    问题: Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequenc ...

  5. debian内核代码执行流程(一)

    本文根据debian开机信息来查看内核源代码. 系统使用<debian下配置dynamic printk以及重新编译内核>中内核源码来查看执行流程. 使用dmesg命令,得到下面的开机信息 ...

  6. 克隆VMware虚拟机

    虚拟机使用过程需要用到多个进行实验.重新安装时间又太长,通过vmware虚拟机自带软件能够很好的快速克隆出完全相同的系统. 环境:关闭VMware,不要在开启状态哦~ 步骤: 选中需要被克隆的系统 菜 ...

  7. qq在线客服代码

    http://wpa.qq.com/msgrd?v=3&uin=1456262869&site=www.cactussoft.cn&menu=yes

  8. MySQL使用FEDERATED engine建立代理表

    CREATE TABLE `yndzm` ( `city` varchar(40) DEFAULT NULL COMMENT '市(州)', `county` varchar(60) DEFAULT ...

  9. webview 最简单的demo

    ) { return; } view.loadUrl(url); }} <!--activity_test.xml> <?xml version="1.0" en ...

  10. poj 2116 Death to Binary? 模拟

    Death to Binary? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1707   Accepted: 529 D ...