1. Description
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.
Note:
The value in the given matrix is in the range of [0, 255].
The length and width of the given matrix are in the range of [1, 150].
2. Runtime Distribution
3. Submission Details
4. Example
Input:
[[1,1,1],
[1,0,1],
[1,1,1]]
Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
5. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active"]
private int[][] dir = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }, { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 }, { 0, 0 } }; public int[][] imageSmoother(int[][] M) { if (M == null || M.length == 0 || M[0].length == 0) { return M; } int rowLength = M.length; int colLength = M[0].length; int[][] result = new int[rowLength][colLength]; for (int i = 0; i < rowLength; i++) { for (int j = 0; j < colLength; j++) { result[i][j] = fill(i, j, rowLength, colLength, M); } } return result; } private int fill(int x, int y, int rowLength, int colLength, int[][] M) { int i = x, j = y; int total = 0; int count = 0; for (int k = 0; k < dir.length; k++) { i = x + dir[k][0]; j = y + dir[k][1]; if (i >= 0 && i < rowLength && j >= 0 && j < colLength) { total++; count += M[i][j]; } } return count / total; }
[/restab]
[/restabs]
6.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active" ]
public class LeetCode0661 { private int[][] dir = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }, { -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 }, { 0, 0 } }; public int[][] imageSmoother(int[][] M) { if (M == null || M.length == 0 || M[0].length == 0) { return M; } int rowLength = M.length; int colLength = M[0].length; int[][] result = new int[rowLength][colLength]; for (int i = 0; i < rowLength; i++) { for (int j = 0; j < colLength; j++) { result[i][j] = fill(i, j, rowLength, colLength, M); } } return result; } private int fill(int x, int y, int rowLength, int colLength, int[][] M) { int i = x, j = y; int total = 0; int count = 0; for (int k = 0; k < dir.length; k++) { i = x + dir[k][0]; j = y + dir[k][1]; if (i >= 0 && i < rowLength && j >= 0 && j < colLength) { total++; count += M[i][j]; } } return count / total; } public static void main(String[] args) { LeetCode0661 leetcode = new LeetCode0661(); int[][] M = new int[][] { { 1, 1, 1 }, { 1, 0, 1 }, { 1, 1, 1 } }; int[][] result = leetcode.imageSmoother(M); for (int i = 0; i < result.length; i++) { for (int j = 0; j < result[0].length; j++) { System.out.print(result[i][j] + " "); } System.out.println(); } } }
[/restab]
[/restabs]
Comments | NOTHING