# C++ some thoughts

• it is painful to consider all corner cases. So I tried another way.

Instead of adding all its neighbors, I tried to add a number to its neighbors. and I can use a similar way to count its neighbors.

``````class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
auto ans = M;
if(M.empty() || M[0].empty()) return ans;
int row = M.size(), col = M[0].size();
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
//down
if(i + 1 < row) ans[i+1][j] += M[i][j];
//up
if(i > 0) ans[i-1][j] += M[i][j];
//left
if(j > 0) ans[i][j-1] += M[i][j];
//right
if(j + 1 < col) ans[i][j+1] += M[i][j];
//left up
if(i > 0 && j > 0) ans[i-1][j-1] += M[i][j];
//left down
if(i+1 < row && j > 0) ans[i+1][j-1] += M[i][j];
//right up
if(i > 0 && j+1 < col) ans[i-1][j+1] += M[i][j];
//right down
if(i+1 < row && j+1 < col) ans[i+1][j+1] += M[i][j];
}
}
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
ans[i][j] /= getCount(i, j, col, row);
return ans;
}

int getCount(int i, int j, int col, int row)
{
int ans = 1;
if(i > 0) ans++; //up
if(i+1 < row) ans++; //down
if(j > 0) ans++; //left
if(j+1 < col) ans++; //right
if(i > 0 && j > 0) ans++; //left up
if(i+1 < row && j > 0) ans++; //left down
if(i > 0 && j+1 < col) ans++; //right up
if(i+1 < row && j+1 < col) ans++; //right down
return ans;
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.