My easy-to-understand C++ solution


  • 0
    A

    class Solution {//速度很快啊,哈哈哈
    public:
    vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
    int n=M.size(); //n是行数
    int m=M[0].size(); //m是列数
    vector<vector<int>> result(n,vector<int>(m,0));
    for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
    int numbers_sum=1,pixels_sum=M[i][j];
    if(i-1>=0 && j-1>=0) {
    pixels_sum+=M[i-1][j-1];
    numbers_sum++;
    }
    if(i-1>=0) {
    pixels_sum+=M[i-1][j];
    numbers_sum++;
    }
    if(i-1>=0 && j+1<m) {
    pixels_sum+=M[i-1][j+1];
    numbers_sum++;
    }
    if(j-1>=0) {
    pixels_sum+=M[i][j-1];
    numbers_sum++;
    }
    if(j+1<m) {
    pixels_sum+=M[i][j+1];
    numbers_sum++;
    }
    if(i+1<n && j-1>=0) {
    pixels_sum+=M[i+1][j-1];
    numbers_sum++;
    }
    if(i+1<n) {
    pixels_sum+=M[i+1][j];
    numbers_sum++;
    }
    if(i+1<n && j+1<m) {
    pixels_sum+=M[i+1][j+1];
    numbers_sum++;
    }
    result[i][j]=pixels_sum/numbers_sum;
    }
    }
    return result;
    }
    };


Log in to reply
 

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