share my straight forward java solution!


  • 2
    T
    class Solution {
        public int[][] imageSmoother(int[][] M) {
             if(M==null || M.length<1){
                 return null;
             }
             int[][] array = new int[M.length][M[0].length];
             int[][] dirs = new int[][]{{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,1},{1,-1}};
             for(int i=0;i<M.length;i++){
                 for(int j=0;j<M[0].length;j++){
                     int sum=M[i][j];
                     int count =1;
                     for(int [] dir:dirs){
                         int x=i+dir[0];
                         int y=j+dir[1];
                         if(x<0 || x>=M.length || y<0 || y>=M[0].length){
                             continue;
                         }else{
                             count++;
                             sum+=M[x][y];
                         }
                     }
                     
                     array[i][j] = sum/count;
                 }
             }
            
             return array;
        }
    }
    

Log in to reply
 

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