LeetCode Image Smoother
原题链接在这里: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:
- The value in the given matrix is in the range of [0, 255].
- 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的更多相关文章
- [LeetCode] 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 (图像平滑器)
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- 【LeetCode】661. Image Smoother 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...
- LeetCode - 661. Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- LeetCode算法题-Image Smoother(Java实现)
这是悦乐书的第282次更新,第299篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第150题(顺位题号是661).给定表示图像灰度的2D整数矩阵M,您需要设计一个平滑器以 ...
- [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 ...
- C#LeetCode刷题之#661-图片平滑器( Image Smoother)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3730 访问. 包含整数的二维矩阵 M 表示一个图片的灰度.你需要 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [Swift]LeetCode661. 图片平滑器 | Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
随机推荐
- 转:ADO,OLEDB,ODBC,DAO的区别
ODBC(Open Database Connectivity,开放数据库互连) 1992年,微软公司开放服务结构(WOSA,Windows Open Services Architecture)中有 ...
- Cuckoo Hash——Hash冲突的解决办法
参考文献: 1.Cuckoo Filter hash算法 2.cuckoo hash 用途: Cuckoo Hash(布谷鸟散列).问了解决哈希冲突的问题而提出,利用较少的计算换取较大的空间.占用空间 ...
- Keepalived 主备配置
keepalived主备或多主多备,配置都是一样配置方法,只是搭建多少的问题. 1.keepalived安装 参考:https://www.cnblogs.com/zwcry/p/9542867.ht ...
- redis操作封装整理
<?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...
- MATLAB更换字体后 中文显示乱码问题
MATLAB的脚本代码默认宋体显示,对于大多数720P的显示器来说,无论是中文还是英文,显示效果都非常差.Windows环境下,Consolas是一种比较理想的英文代码字体. Consolas 字体是 ...
- springmvc异常处理(非注解与注解)
1.异常 程序中的异常一般分为两类:预期异常,运行时异常.前者是我们可预知的,我们一般通过捕获和抛出方式处理这些异常.后者主要通过代码规范.测试等手段来减少异常的发生.一般,我们在系统的DAO.Ser ...
- vc 改变控制台字符颜色
#include <Windows.h> #include <stdio.h> #include <iostream> using namespace std; i ...
- js的值类型和引用类型
(1)值类型:String.Boolean.Number.null.undefined.(原始值) var a = 2; var b = a; b=3; a ==>2; b ==>3 原 ...
- Valid Parentheses有效括号匹配。利用栈。
问题描述:给定一个字符串,其中只包含字符‘{’, '}', '[', ']', '(', ')'确定如果输入字符串是有效的.括号必须以正确的顺序排列,“()”和“()[]{ ...
- keystone DB in devstack
~$ mysql -u -p root mysql> use keystone; mysql> show tables;+------------------------+| Tables ...