Created
August 29, 2017 21:42
-
-
Save cixuuz/c59c3895e6ae9bf2c54bd5bad98259d7 to your computer and use it in GitHub Desktop.
[661. Image Smoother] #leetcode
This file contains hidden or 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) { | |
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