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个单元和它本身的值求平均,如果周围的单元 ...
随机推荐
- cnzz友盟怎么安装网站统计代码监控网站流量
做网站的都知道cnzz统计,它是用来统计网站流量的,可以分析网站数据,进行更好的对网站优化,下面我教大家怎么添加统计代码 工具/原料 cnzz 方法/步骤 打开百度搜索"cnzz友盟&quo ...
- Eclipse配置maven环境
一.什么是maven? Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个 ...
- 小白的Python之路 day5 logging模块
logging模块的特点及用法 一.概述 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你 ...
- 【开发技术】如何查看项目中struts的版本
struts-configer.xml(struts1)或struts.xml(struts2)中 struts-2.0.dtd处表示版本号
- UltraEdit激活方法
按照UltraEdit 并下载注册机后. 打开UltraEdit 弹出产品是使用 然后点击 输入注册码, 重点:断开网络 点击激活,弹出 离线激活选项. 用户名密码随意输入,打开注册机,把下面的 ...
- js双向绑定和地址传递带来的痛苦解决方案之对象拷贝
function cloneObj(obj) { var newObj = {}; if (obj instanceof Array) { newObj = []; } for (var key in ...
- 异步式I/O与实践式编程
阻塞 线程在执行中如果遇到磁盘读写或网络通信(统称为I/O操作)通常要消耗很长时间 这时操作系统会剥夺这个线程的CPU控制权,使其暂停执行,同时将资源让给其他工作线程 异步I/O 非阻塞IO 针对所有 ...
- 如何导入css和js?
导入css <link rel="stylesheet" href="css路径" type="text/css"> 导入js, ...
- [Qt Quick] No rule to make target问题解决办法
[问题描述] 修改项目中资源的qml文件名或删除无用资源文件后,重新构建项目时,会出现类似如下的问题提示: No rule to make target 'aaa', needed by 'bbb'. ...
- 一、Python表达式基础
Python 能执行简单的计算器的功能: 如>>2+2 ==> 4,1/2==>0.5或者这样写1/2.0==>0.5(取浮点型) 1//2 ==>0 (" ...