Java O(N^2) time complexity, O(1) space


  • 0
    A
    public int[][] imageSmoother(int[][] M) {
        if(M==null || M.length==0 || M[0].length==0) return M;
        for(int i=0; i<M.length; i++){
            for(int j=0; j<M[i].length; j++){
                int row = i, col = j, num = 0, cnt = 0;
                for(int r=-1; r<=1; r++){
                    for(int c=-1; c<=1; c++){
                        row = i+r; col = j+c;
                        if(row<0 || row>=M.length || col<0 ||col>=M[0].length) continue;
                        cnt++;
                        num += M[row][col] % 256;
                    }
                }
                M[i][j] += num/cnt << 8;
            }
        }
        for(int i=0; i<M.length; i++){
            for(int j=0; j<M[i].length; j++){
                M[i][j] >>= 8;
            }
        }
        return M;
    }

Log in to reply
 

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