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% ...
随机推荐
- webStrom中配置nodejs
1.安装nodejs 下载地址:node.js:https://nodejs.org/download/ 按照提示安装即可 2.安装WebStrom 按照提示安装即可 下载地址:webstorm:ht ...
- Ethical Hacking - NETWORK PENETRATION TESTING(23)
Detecting ARP Posionning Attacks ARP main security issues: 1. Each ARP requests/response is trusted. ...
- ES6语法——let和const
一.let 1.定义 ES6新增了let命令,用来声明变量,用法类似于var,但是和var有一定的区别 2.let只在块级作用域内有效 首先来看一个比较简单的例子,请告诉我,他们分别输出什么 //代码 ...
- MySQL(三)视图
视图:view,是一种有结构(有行有列)但是没有结果(结构中不真实的存放数据)的虚拟表,虚拟表的结构来源不是自己定义,而是从对应的基表中产生(视图的数据来源): 视图意义: 1.视图可以节省SQL语句 ...
- 面试题五十七:和为s的数字
题目一:和为s的数字,在一个递增数组中寻找两个数字的和等于s 方法:双指针法,一个在头一个在尾:如果两个指针指向的和小于,那么be++:大于end--: 题目二:打印所有和为s的连续正数序列 方法:双 ...
- 深入理解JVM(③)Java的锁优化
前言 从JDK5到JDK6HotSpot虚拟机开发团队花费了大量的资源实现了各种锁优化技术,如适应性自旋(Adaptive Spinning).锁消除(Lock Elimination).锁膨胀(Lo ...
- java基础(七)--基本类型转换
一.转换规则 1.类型转换的原则是: 小容量可以自动转成大容量,大容量转成小容量,需要强制转换,有些类型之前不能转换 判断以下语句是否符合要求 2.默认的识别数字 整数默认->int 浮点数默认 ...
- Ansible部署zabbix-agent
playbook目录 zabbix/ ├── hosts ##定义的主机列表 ├── install_zabbix_agent.yml ##安装入口文件 └── roles ├── install_z ...
- parted分区
((parted)mklabel ----创建磁盘标签New disk labeltype? gpt ---输入磁盘表情名(parted) p ----再次列出磁盘分区 (parted) mkpart ...
- iframe和DataForm
一.iframe使用 iframe在一个页面中,相当于整个window窗口的子窗口,可通过页面的元素结构查看. <div> <p>学习iframe</p> < ...