当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- 华为高层谈 35 岁危机,程序员如何破年龄之忧?
- How to write the summary? (including 7 easily ignored precautions and a summary of common sentence patterns in 80% of review articles)
- Acwing brush questions
- Firewall command simple operation
- How to make your language academic when writing a thesis? Just remember four sentences!
- Share | 2022 big data white paper of digital security industry (PDF attached)
- 智装时代已来,智哪儿邀您一同羊城论剑,8月4日,光亚展恭候
- (翻译)网站流程图和用户流程图的使用时机
- 吴恩达机器学习课后习题——线性回归
- Recommendation | scholar's art and Tao: writing papers is a skill
猜你喜欢

Recommendation | DBT skills training manual: baby, you are the reason why you live

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

Sentinel fusing and current limiting

低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!

Pathmatchingresourcepatternresolver parsing configuration file resource file

生活相关——十年的职业历程(转)
![Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130](/img/84/e5cb5199fe4602440b50dfc4afe963.gif)
Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130

PathMatchingResourcePatternResolver解析配置文件 资源文件

华为再发“天才少年”全球召集令,有人曾放弃360万年薪加入

零售连锁门店收银系统源码管理商品分类的功能逻辑分享
随机推荐
Acwing刷题
雅迪高端之后开始变慢
I.MX6U-ALPHA开发板(GPIO中断实验)
Leetcode:1184. Distance between bus stops -- simple
图互译模型
(翻译)网站流程图和用户流程图的使用时机
How to write the abbreviation of the thesis?
JS get some attributes of the object
Go plus security: an indispensable security ecological infrastructure for build Web3
How to transfer English documents to Chinese?
生活相关——一个华科研究生导师的肺腑之言(主要适用于理工科)
Trust sums two numbers
ROS2的launch有何不同?
Constructing verb sources for relation extraction
Dynamic planning for stair climbing
How to write abstract in English thesis?
Segger embedded studio cannot find xxx.c or xxx.h file
吴恩达机器学习课后习题——线性回归
operator new、operator delete补充讲义
When you try to delete all bad code in the program | daily anecdotes