Short and Straightforward C++ Solution


  • 0
    Y

    ...
    class Solution {
    public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {

        int row = M.size();
        int col = M[0].size();
        vector<vector<int>> res;
        vector<int> onerow (col,0);
        for(int i=0; i<row; i++)
            res.push_back(onerow);
        
        int sum, count;
        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                sum = count = 0;
                for(int x=max(i-1,0); x<=min(i+1,row-1); x++){
                    for(int y=max(j-1,0); y<=min(j+1,col-1); y++){
                        sum += M[x][y];
                        count += 1;
                    }
                }
                res[i][j] = sum/count;
            }
        }
        return res;
    }
    

    };
    ...


Log in to reply
 

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