Java solution O(M)


  • 0
    class Solution {
        public int[][] imageSmoother(int[][] M) {
            int [][] N = new int [M.length][M[0].length];
            int row = M.length;
            int col = M[0].length;
            for(int i = 0; i < row; i++)
                for(int j = 0; j < col; j++) {
                    int avg = 0;
                    int sum = 0;
                    for(int m = i - 1; m <= i + 1; m++)
                        if(m >= 0 && m < row) 
                            for(int n = j - 1; n <= j + 1; n++) 
                                if(n >= 0 && n < col) {
                                    sum += M[m][n];
                                    avg++;
                                }
                    N[i][j] = sum / avg;
                }
            return N;
        }
    }
    

Log in to reply
 

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