JAVA


  • 0
    Z

    class Solution {
    public int[][] imageSmoother(int[][] M) {
    int length=M[0].length;
    int height=M.length;
    if(length==1&&height==1){
    return M;
    }
    int[][] M1=new int[height][length];
    if(height==1){
    M1[0][0]=(int)Math.floor((M[0][0]+M[0][1])/(float)2);
    M1[0][length-1]=(int)Math.floor((M[0][length-2]+M[0][length-1])/(float)2);
    for(int y=1;y<length-1;y++){
    M1[0][y]=(int)Math.floor((M[0][y-1]+M[0][y]+M[0][y+1])/(float)3);
    }
    return M1;
    }
    if(length==1){
    M1[0][0]=(int)Math.floor((M[0][0]+M[1][0])/(float)2);
    M1[height-1][0]=(int)Math.floor((M[height-1][0]+M[height-2][0])/(float)2);
    for(int x=1;x<height-1;x++){
    M1[x][0]=(int)Math.floor((M[x-1][0]+M[x][0]+M[x+1][0])/(float)3);
    }
    return M1;
    }
    M1[0][0]=(int)Math.floor((M[0][0]+M[0][1]+M[1][0]+M[1][1])/(float)4);
    M1[0][length-1]=(int)Math.floor((M[0][length-1]+M[1][length-1]+M[0][length-2]+M[1][length-2])/(float)4);
    M1[height-1][0]=(int)Math.floor((M[height-1][0]+M[height-2][0]+M[height-1][1]+M[height-2][1])/(float)4);
    M1[height-1][length-1]=(int)Math.floor((M[height-1][length-1]+M[height-2][length-1]+M[height-1][length-2]+M[height-2][length-2])/(float)4);
    for(int y=1;y<length-1;y++){
    M1[0][y]=(int)Math.floor((M[0][y]+M[0][y-1]+M[0][y+1]+M[1][y]+M[1][y-1]+M[1][y+1])/(float)6);
    }
    for(int y=1;y<length-1;y++){
    M1[height-1][y]=(int)Math.floor((M[height-1][y]+M[height-1][y-1]+M[height-1][y+1]+M[height-2][y]+M[height-2][y-1]+M[height-2][y+1])/(float)6);
    }
    for(int x=1;x<height-1;x++){
    M1[x][0]=(int)Math.floor((M[x][0]+M[x-1][0]+M[x+1][0]+M[x][1]+M[x-1][1]+M[x+1][1])/(float)6);
    }
    for(int x=1;x<height-1;x++){
    M1[x][length-1]=(int)Math.floor((M[x][length-1]+M[x-1][length-1]+M[x+1][length-1]+M[x][length-2]+M[x-1][length-2]+M[x+1][length-2])/(float)6);
    }
    for(int y=1;y<length-1;y++){
    for(int x=1;x<height-1;x++){
    M1[x][y]=(int)Math.floor((M[x][y]+M[x][y-1]+M[x][y+1]+M[x-1][y-1]+M[x-1][y]+M[x-1][y+1]+M[x+1][y-1]+M[x+1][y]+M[x+1][y+1])/(float)9);
    }
    }
    return M1;
    }
    }


Log in to reply
 

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