# 661.Image Smoother

661.Image Smoother

``````示例 1:

[[1,1,1],
[1,0,1],
[1,1,1]]

[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

``````class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int r=M.size();
int c=M[0].size();
if(r==1 && c==1) return M;
vector<vector<int>> res(M);
if(r==1)
{
res[0][0]=(M[0][0]+M[0][1])/2;
res[0][c-1]=(M[0][c-1]+M[0][c-2])/2;
for(int i=1;i<c-1;i++)
res[0][i] = (M[0][i] + M[0][i-1] +M[0][i+1] )/3;
return res;
}
if(c==1)
{
res[0][0]=(M[0][0]+M[1][0])/2;
res[r-1][0]=(M[r-1][0]+M[r-2][0])/2;
for(int i=1;i<r-1;i++)
res[i][0] = (M[i][0] + M[i-1][0] +M[i+1][0])/3;
return res;
}

for(int i=1;i<r-1;i++)
for(int j=1;j<c-1;j++)
res[i][j] =(M[i][j] +M[i-1][j]+ M[i][j-1] + M[i-1][j-1]+ M[i][j+1] + M[i+1][j] + M[i+1][j+1] + M[i-1][j+1] + M[i+1][j-1])/9;
for(int i=1;i<c-1;i++)
res[0][i] = (M[0][i] + M[0][i-1] +M[0][i+1] +M[1][i] + M[1][i-1]+M[1][i+1])/6;
for(int i=1;i<c-1;i++)
res[r-1][i] = (M[r-1][i] + M[r-1][i-1] +M[r-1][i+1] +M[r-2][i] + M[r-2][i-1]+M[r-2][i+1])/6;
for(int i=1;i<r-1;i++)
res[i][0] = (M[i][0] + M[i-1][0] +M[i+1][0] +M[i][1] + M[i-1][1]+M[i+1][1])/6;

for(int i=1;i<r-1;i++)
res[i][c-1] = (M[i][c-1] + M[i-1][c-1] +M[i+1][c-1] +M[i][c-2] + M[i-1][c-2]+M[i+1][c-2])/6;

res[0][0]=(M[0][0]+M[0][1] +M[1][1]+M[1][0])/4;
res[0][c-1]=(M[0][c-1]+M[0][c-2]+M[1][c-1]+M[1][c-2])/4;
res[r-1][0]=(M[r-1][0]+M[r-1][1]+M[r-2][0]+M[r-2][1])/4;
res[r-1][c-1]=(M[r-1][c-1]+M[r-2][c-1]+M[r-2][c-2]+M[r-1][c-2])/4;
return res;
}

};``````

Last updated