Skip to content

Instantly share code, notes, and snippets.

@cc2011
Created August 22, 2017 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cc2011/0e2fb6158a12d59738df3a2947859cfb to your computer and use it in GitHub Desktop.
Save cc2011/0e2fb6158a12d59738df3a2947859cfb to your computer and use it in GitHub Desktop.
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