problem
题意:其实类似于图像处理的均值滤波。
solution:
妙处在于使用了一个dirs变量来计算邻域数值,看起来更简洁!
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
if(M.empty() || M[].empty()) return {};
vector<vector<int>> res = M, dirs = {{-, -}, {-, }, {-, }, {, -},
{, }, {, -}, {, }, {, } };//dirs err.
int m = M.size(), n = M[].size();
for(int i=; i<m; i++)
{
for(int j=; j<n; j++)
{
int sum = M[i][j], num = ;//err.
for(auto dir : dirs)
{
int x = i+dir[], y = j+dir[];
if(x< || x>=m || y< || y>=n) continue;//err.
num++;
sum += M[x][y];
}
res[i][j] = sum / num;
} }
return res; }
};
参考
1. Leetcode_easy_661. Image Smoother;
完