Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created August 29, 2017 21:42
Show Gist options
  • Save cixuuz/c59c3895e6ae9bf2c54bd5bad98259d7 to your computer and use it in GitHub Desktop.
Save cixuuz/c59c3895e6ae9bf2c54bd5bad98259d7 to your computer and use it in GitHub Desktop.
[661. Image Smoother] #leetcode
class Solution {
public int[][] imageSmoother(int[][] M) {
if (M == null) return null;
int rows = M.length;
if (rows == 0) return new int[0][];
int cols = M[0].length;
int result[][] = new int[rows][cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int count = 0;
int sum = 0;
for (int incR : new int[]{-1, 0, 1}) {
for (int incC : new int[]{-1, 0, 1}) {
if (row + incR >= 0 && row + incR < rows && col + incC >= 0 && col + incC < cols) {
count++;
sum += M[row + incR][col + incC];
}
}
}
result[row][col] = sum / count;
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment