C#LeetCode刷题之#661-图片平滑器( Image Smoother)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3730 访问。
包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们。
输入:
[[1,1,1],
[1,0,1],
[1,1,1]]输出:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]解释:
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
注意:
给定矩阵中的整数范围为 [0, 255]。
矩阵的长和宽的范围均为 [1, 150]。
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.
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].
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3730 访问。
public class Program {
public static void Main(string[] args) {
int[,] nums = null;
nums = new int[,] { { 1, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 } };
var res = ImageSmoother(nums);
ShowArray(res);
Console.ReadKey();
}
private static void ShowArray(int[,] array) {
foreach(var num in array) {
Console.Write($"{num} ");
}
Console.WriteLine();
}
private static int[,] ImageSmoother(int[,] M) {
int[,] result = M.Clone() as int[,];
for(int i = 0; i < M.GetLength(0); i++) {
for(int j = 0; j < M.GetLength(1); j++) {
result[i, j] = ComputerSmoothValue(M, i, j);
}
}
return result;
}
private static int ComputerSmoothValue(int[,] M, int i, int j) {
//以某个数为中心,求9个数的平均值即可
//该题主要考查边界的处理
int count = 0;
int sum = 0;
for(int m = Math.Max(i - 1, 0); m <= Math.Min(i + 1, M.GetLength(0) - 1); m++) {
for(int n = Math.Max(j - 1, 0); n <= Math.Min(j + 1, M.GetLength(1) - 1); n++) {
count++;
sum += M[m, n];
}
}
return sum / count;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3730 访问。
0 0 0 0 0 0 0 0 0
分析:
假设原二维数组有m行,n列,ComputerSmoothValue的执行次数在最坏的情况会执行9次,ComputerSmoothValue执行的次数为m*n次,故以上算法的时间复杂度为: 。
C#LeetCode刷题之#661-图片平滑器( Image Smoother)的更多相关文章
- Java实现 LeetCode 661 图片平滑器(暴力)
661. 图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元 ...
- Leetcode 661.图片平滑器
图片平滑器 包含整数的二维矩阵 M 表示一个图片的灰度.你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个 ...
- [Swift]LeetCode661. 图片平滑器 | Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- LeetCode刷题总结-树篇(中)
本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...
- 看完互联网大佬的「LeetCode 刷题手册」, 手撕了 400 道 Leetcode 算法题
大家好,我是 程序员小熊 ,来自 大厂 的程序猿.相信绝大部分程序猿都有一个进大厂的梦想,但相较于以前,目前大厂的面试,只要是研发相关岗位,算法题基本少不了,所以现在很多人都会去刷 Leetcode ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
随机推荐
- Mysql---搭建简单集群,实现主从复制,读写分离
参考博客:https://blog.csdn.net/xlgen157387/article/details/51331244 A. 准备:多台服务器,且都可以互相随意访问root用户,都可以随意进行 ...
- Python Ethical Hacking - BACKDOORS(6)
File Upload: A file is a series of characters. Uploading a file is the opposite of downloading a fil ...
- 主席树铺垫——总区间第k小
题目描述(口糊) 先给定一个长度为n的数列,然后给m次操作,每次输入b,求第b小的数. 样例输入 5 7 4 10 9 23 5 1 2 3 4 5 样例输出 4 7 9 10 23 数据范围及温馨提 ...
- 技能实际操作:如何为Centos7 配置静态路由?
如图: 业务地址:192.168.10.0/24 ---- 192.168.20.0/24 管理地址:172.168.10.0/24 --- 172.168.20.0/24 需求:每台主机配置两张网卡 ...
- 设计模式:proxy模式
目的:为其他对象提供一种代理以控制对这个对象的访问 理解:尽管Decorator的实现部分与代理相似,但Decorator的目的不一样.Decorator为对象添加一个或多个功能,而代理则控制对对象的 ...
- 题解 洛谷 P4632 【[APIO2018] New Home 新家】
首先考虑可以用二分答案来解决询问,可以二分一个长度\(len\),若在区间\([x-len,x+len]\)内包含了所有\(k\)种的商店,那么这个\(len\)就是合法的,可以通过二分来求其最小值. ...
- 2.pandas的数据结构
对于文件来说,读取只是最初级的要求,那我们要对文件进行数据分析,首先就应该要知道,pandas会将我们熟悉的文件转换成了什么形式的数据结构,以便于后续的操作 数据结构 pandas对文件一共有两种数据 ...
- Django开发之Datetime类型JSON序列化时报错
前提回顾 在进行django开发view视图时,如果数据库字段是 datetime类型,在JSON序列化返回时,会出现异常 异常现象 TypeError: Object of type datetim ...
- django 命令行命令
django-admin startproject 项目名 django-admin startproject python manage.py makemigrations python manag ...
- Python重命名和删除文件
Python重命名和删除文件: rename(当前的文件名,新文件名): 将当前的文件名修改为新文件名 程序: # os.rename('旧名字',’新名字‘) import os os.rename ...