Simple Java Solution


  • 0
    A
    public int[][] imageSmoother(int[][] M) {
            int rows = M.length;
            int cols = M[0].length;
            int [][] output = new int[rows][cols];
            
            for(int i = 0; i < rows; i++){
                for(int j = 0; j<cols;j++){
                    output[i][j] = search(M, i, j, 0, 0, rows, cols);
                }
            }
            
            return output;
        }
        
        public int search(int[][] M, int m, int n, int sum, int div, int rows, int cols){
            for(int i = m-1; i<=m+1;i++){
                for(int j= n-1; j<=n+1;j++){
                     if(i>=0 && j>=0 && i<=rows-1 && j<=cols-1){
                        sum+=M[i][j];
                        div=div+1;
                     }
                }
            }
            
            return (int) Math.floor(sum/div);
        }
    

Log in to reply
 

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