算法1
注意边界即可。
时间复杂度 $O(mn)$
C++ 代码
class Solution {
public:
int getd(vector<vector<int>>& M,int x,int y){
int x1 = x - 1 < 0 ? 0:x-1;
int y1 = y - 1 < 0 ? 0:y-1;
int x2 = x + 1 > M.size() - 1 ? M.size() - 1:x + 1;
int y2 = y + 1 > M[0].size() - 1 ? M[0].size() - 1 : y + 1;
int n = (x2 - x1 + 1)*(y2 - y1 + 1);
int sum = 0;
for(int i = x1 ;i <= x2;++i){
for(int j = y1; j <= y2;++j){
sum += M[i][j];
}
}
return sum/n;
}
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
vector<vector<int>>ans;
for(int i =0;i < M.size();++i){
vector<int>t;
for(int j =0;j < M[0].size();++j){
int ts = getd(M,i,j);
t.push_back(ts);
}
ans.push_back(t);
}
return ans;
}
};
```