TypechoJoeTheme

IT技术分享

统计

[LeetCode 661] Image Smoother [Java] [Runtime : 27MS]

2017-09-26
/
0 评论
/
807 阅读
/
正在检测是否收录...
09/26

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

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;
}

6.Test

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();
        }
    }
}
Matrix
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/964/(转载时请注明本文出处及文章链接)