Super readable Java clear code


  • 0
    class Solution {
        public int[][] imageSmoother(int[][] M) {
            if(M == null || M.length == 0 || M[0].length == 0)
                return null;
            int[][] res = new int[M.length][M[0].length];
            int[] x = {1, -1, 0, 0, 1, -1, 1, -1};
            int[] y = {0, 0, 1, -1, 1, -1, -1, 1};
            for(int i = 0; i < M.length; i++){
                for(int j = 0; j < M[0].length; j++){
                    int accu = M[i][j], count = 1;
                    for(int k = 0; k < 8; k++){
                        if(i + x[k] < 0 || i + x[k] >= M.length || j + y[k] < 0 || j + y[k] >= M[0].length){
                            continue;
                        }
                        count++;
                        accu += M[i+x[k]][j+y[k]];
                    }
                    res[i][j] = accu/count; 
                }
            }
            return res;  
        }
    }
    

Log in to reply
 

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