当前位置:网站首页>力扣每日一题-第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;
}
};边栏推荐
- Graph translation model
- Overview of wavelet packet transform methods
- What are the duplicate check rules for English papers?
- 2.9.4 Boolean object type processing and convenient methods of ext JS
- Dijikstra (preprocessing first) +dfs, relocation truncated to fit
- How to make your language academic when writing a thesis? Just remember four sentences!
- sorting and searching 二分查找法
- Acwing刷题
- Verilog implementation of key dithering elimination
- 综合评价与决策方法
猜你喜欢

MATLAB绘图

redux

PathMatchingResourcePatternResolver解析配置文件 资源文件

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

HelloWorld case analysis

What are the duplicate check rules for English papers?

makefile知识再整理(超详细)

Soft simulation rasterization renderer

Seat / safety configuration upgrade is the administrative experience of the new Volvo S90 in place

Overview of wavelet packet transform methods
随机推荐
Share | 2022 big data white paper of digital security industry (PDF attached)
图互译模型
LeetCode:1184. 公交站间的距离————简单
Synchronous FIFO based on shift register
荐书 |《学者的术与道》:写论文是门手艺
Method of test case design: introduction, trial recruit, preliminary exploration of equivalence boundary
Go Plus Security:一款Build Web3不可或缺的安全生态基础设施
1. Mx6u-alpha development board (GPIO interrupt experiment)
Functions of anonymous functions
Pat class a 1039 course list for student
(translation) timing of website flow chart and user flow chart
Implementation of distributed lock
荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
远坂凛壁纸
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
Life related - ten years of career experience (turn)
Sorting and searching
雅迪高端之后开始变慢
Operator new, operator delete supplementary handouts
Helloworld案例分析