C++ easy to understand simple code


  • 1
    class Solution {
    public:
        vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
            if (M.empty()) return {};
            vector<vector<int> > temp(M.size(), vector<int>(M[0].size(), 0));
            
            for (int i=0;i<M.size();i++){
                for (int j=0;j<M[0].size();j++){
                        int sum=0, count=0;
                        for (int m=-1;m<=1;m++){
                            for (int n=-1;n<=1;n++){
                                int x=i+m, y=j+n;
                                if (x>=M.size() || x<0 || y>=M[0].size() || y<0) continue;
                                sum += M[x][y];
                                count++;
                            }
                        }
                        temp[i][j] = sum/count;
                }
            }
            return temp;
        }
    };
    

Log in to reply
 

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