LeetCode - 661. Image Smoother
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的更多相关文章
- LeetCode 661. Image Smoother (图像平滑器)
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 【Leetcode_easy】661. Image Smoother
problem 661. Image Smoother 题意:其实类似于图像处理的均值滤波. solution: 妙处在于使用了一个dirs变量来计算邻域数值,看起来更简洁! class Soluti ...
- 【LeetCode】661. Image Smoother 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...
- [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 ...
- [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 ...
- 661. Image Smoother色阶中和器
[抄题]: Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...
- 661. Image Smoother@python
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- Java实现 LeetCode 661 图片平滑器(暴力)
661. 图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元 ...
随机推荐
- 使用notepad++作为keil的外部编辑器
之前一直不喜欢keil的编辑界面,但是又不想太浮夸.看到很多群里有人用vscode写stm32的序,但是直接用vscode编写的花,各种设置很麻烦.而且还不能调试.于是想到有没有一个轻便简约的外部编辑 ...
- phpmyadmin设置密码,不用登录直接进入
版权声明:本文为博主原创文章,未经博主允许不得转载. 1.config.sample.inc.PHP改为config.inc.php 2.加入或更改代码: [php] view plain copy ...
- Mac下Charles Contents乱码解决办法
用到Charles,下载最新的4.0.1版本,但是发生乱码问题.百度好久才找到个靠谱的,那些说什么在Info.plist文件加字符串的,都是假的,反正我是试了都没用,这里记下详细的操作步骤解决: 1. ...
- 1.MAVEN项目的创建与问题的解决
一.创建一个maven-webapp.(环境:mac和15版本的IDEA) 二.next--->填写groupId(公司单位的名字,你组织的名字)和ArtifactID(有关tomcat,以后用 ...
- 2017-07-11(sync basename dirname )
sync 在内存中尚未被更新的数据,就会被写入硬盘中:在关机或者重启前,最好多执行几次! basename 获取文件名 dirname 获取目录名
- 非线性规划带约束-scipy.optimize.minimize
# coding=utf-8 from scipy import optimize import numpy as np def get(args): a, b, c, d, e, f, g, h = ...
- python_判断变量类型
需求: 已知有一个变量,我想对他进行预处理判断,如果这个变量是字符串,则在字符串后面加上后缀'_str',如果整形就让其加5,还比如我要求这个变量是整形或者字符串,都行 如何做? #!/usr/bin ...
- python_斐波那契数列
什么是斐波那契数列? -- 一组第从第三个值开始,每个值都等于前两个值之和的一种有意思的数列 如[1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 如何用程序进行实现? -- 逻辑整 ...
- js 数组与对象的区别
学习javascript的时候,我曾经一度搞不清楚”数组”(array)和”对象”(object)的根本区别在哪里,两者都可以用来表示数据的集合. 比如有一个数组a=[1,2,3,4],还有一个对 ...
- JavaScript之图片懒加载的实现
图片懒加载指的是在浏览过程中随着需要才被加载出来,例如某宝上面浏览商品时,会伴随很多的图片,如果一次全部加载出来的话,显然资源有些浪费,并且加载速度也会相对降低,那么懒加载的实现很重要.即随着浏览翻阅 ...