C++ AC Solution


  • 0

    Just try to be straight forward. Thanks for my girl's advise.

    class Solution {
    public:
        vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
            int m_sz = M.size(), sz = M[ 0 ].size();
            vector<vector<int>> ans( m_sz, vector<int>( sz, 0 ) );
            for ( int i = 0; i < m_sz; ++i ) {
                for ( int j = 0; j < sz; ++j ) {
                    int sum = 0;
                    int div = 0;
                    if ( i - 1 >= 0 ) { sum += M[ i - 1][ j ]; ++div; }
                    if ( j - 1 >= 0 ) { sum += M[ i ][ j - 1]; ++div; }
                    if ( i + 1 != m_sz ) { sum += M[ i + 1][ j ]; ++div; }
                    if ( j + 1 != sz ) { sum += M[ i ][ j + 1 ]; ++div; }
                    if ( i - 1 >= 0 && j - 1 >= 0 ) { sum += M[ i - 1 ][ j - 1 ]; ++div;}
                    if ( i - 1 >= 0 && j + 1 != sz ) { sum += M[ i - 1 ][ j + 1 ]; ++div;}
                    if ( i + 1 != m_sz && j - 1 >= 0 ) { sum += M[ i + 1 ][ j - 1 ]; ++div;}
                    if ( i + 1 != m_sz && j + 1 != sz ) { sum += M[ i + 1 ][ j + 1 ]; ++div;}
                    ans[ i ][ j ] = ( sum + M[ i ][ j ] ) / ( div + 1 );
                }
            }
            return ans;
        }
    };//162ms
    

    BTW why i cant entry discuss section of this problem by click the "Discuss" button?


Log in to reply
 

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