当前位置:网站首页>力扣每日一题-第42天-661. 图片平滑器
力扣每日一题-第42天-661. 图片平滑器
2022-07-26 04:16:00 【重邮研究森】
2022.7.25今天你刷题了吗?
题目:
图像平滑器 是大小为 3 x 3 的过滤器,用于对图像的每个单元格平滑处理,平滑处理后单元格的值为该单元格的平均灰度。
每个单元格的 平均灰度 定义为:该单元格自身及其周围的 8 个单元格的平均值,结果需向下取整。(即,需要计算蓝色平滑器中 9 个单元格的平均值)。
如果一个单元格周围存在单元格缺失的情况,则计算平均灰度时不考虑缺失的单元格(即,需要计算红色平滑器中 4 个单元格的平均值)。

分析:
一个二维数组,求出每个格子它周围有效格子(有值的)的平均值,然后把每个二维数组元素的平均值求出来构造新的二维数组。
思路:先便利一下二维数组,对于第一个【0】【0】下标元素,我们找它的周围8个格子,然后找【0】【1】周围的8个格子,对这8个格子判断是否有效然后求出平均值。
解析:
1.暴力法
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
int m = img.size(), n = img[0].size();
vector<vector<int>> ret(m, vector<int>(n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int num = 0, sum = 0;
for (int x = i - 1; x <= i + 1; x++) {
for (int y = j - 1; y <= j + 1; y++) {
if (x >= 0 && x < m && y >= 0 && y < n) {
num++;
sum += img[x][y];
}
}
}
ret[i][j] = sum / num;
}
}
return ret;
}
};边栏推荐
- 支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布
- 规则引擎Drools的使用
- 图互译模型
- [digital ic/fpga] Hot unique code detection
- Method of test case design: introduction, trial recruit, preliminary exploration of equivalence boundary
- [cloud native] talk about the understanding of the old message middleware ActiveMQ
- Pits encountered by sdl2 OpenGL
- [Reading Notes - > data analysis] 01 introduction to data analysis
- (翻译)按钮位置约定能强化用户使用习惯
- Retail chain store cashier system source code management commodity classification function logic sharing
猜你喜欢

智装时代已来,智哪儿邀您一同羊城论剑,8月4日,光亚展恭候

Pat class a 1039 course list for student

Acwing_12. 背包问题求具体方案_dp

(翻译)网站流程图和用户流程图的使用时机
![[cloud native] talk about the understanding of the old message middleware ActiveMQ](/img/70/fe2ef0bea10d8275c0fe4c8139b027.png)
[cloud native] talk about the understanding of the old message middleware ActiveMQ

This article takes you to graph transformers

Segger embedded studio cannot find xxx.c or xxx.h file

当你尝试删除程序中所有烂代码时 | 每日趣闻

综合评价与决策方法

荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
随机推荐
When you try to delete all bad code in the program | daily anecdotes
(translation) the button position convention can strengthen the user's usage habits
AcWing. 102 best cattle fence
VM虚拟机 没有未桥接的主机网络适配器 无法还原默认配置
How to choose the key words of the thesis?
5 years, 1.4W times, NFT og's road to immortality Web3 column
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
Dynamic planning for stair climbing
Pathmatchingresourcepatternresolver parsing configuration file resource file
Implementation of distributed lock
Life related - less expectation, happier
dijango学习
firewall 命令简单操作
Uniapp pit filling Tour
Graph theory: topological sorting
How to make your language academic when writing a thesis? Just remember four sentences!
Method of test case design: introduction, trial recruit, preliminary exploration of equivalence boundary
Soft simulation rasterization renderer
What model is good for the analysis of influencing factors?
Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree