Created
August 22, 2017 05:18
-
-
Save cc2011/0e2fb6158a12d59738df3a2947859cfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[][] imageSmoother(int[][] M) { | |
int n = M.length; | |
int m = M[0].length; | |
int[][] res = new int[n][m]; | |
int[][] dir = {{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,1},{1,-1},{0,0}}; | |
for(int i=0; i < n; i++) { | |
for(int j=0; j < m; j++) { | |
int count = 0; | |
for(int k=0; k < dir.length; k++) { | |
if(process(res, M,i+dir[k][0], j+dir[k][1], i, j, n, m)) { | |
count++; | |
} | |
} | |
res[i][j] = res[i][j]/count; | |
} | |
} | |
return res; | |
} | |
public boolean process(int[][] res, int[][] M, int x, int y, int currX, int currY, int n, int m) { | |
if(x < 0 || x >= n) { | |
return false; | |
} | |
if(y < 0 || y >= m) { | |
return false; | |
} | |
res[currX][currY] += M[x][y]; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment