C++ Accepted, Easy and 2 minute code


  • 0
    A

    class Solution {
    public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
    vector<vector<int>> ans;
    for(int i=0;i<M.size();i++) {
    vector<int> temp;
    for(int j=0;j<M[i].size();j++){
    int r=0,n=0;
    if (i>0 && j>0) {
    r+=M[i-1][j-1]; n++;
    }
    if (i>0 && j>=0) {
    r+=M[i-1][j]; n++;
    }
    if (i>0 && j<M[i-1].size()-1) {
    r+=M[i-1][j+1]; n++;
    }
    if (i>=0 && j>0) {
    r+=M[i][j-1]; n++;
    }
    if (i>=0 && j<M[i].size()-1) {
    r+=M[i][j+1]; n++;
    }
    if (i<M.size()-1 && j>0) {
    r+=M[i+1][j-1]; n++;
    }
    if (i<M.size()-1 && j>=0) {
    r+=M[i+1][j]; n++;
    }
    if (i<M.size()-1 && j<M[i+1].size()-1) {
    r+=M[i+1][j+1]; n++;
    }
    if (i>=0 && j>=0) {
    r+=M[i][j]; n++;
    }
    // n=1;
    temp.push_back(floor(r/n));
    }
    ans.push_back(temp);
    }
    return ans;
    }
    };


Log in to reply
 

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