Snapshot Solution Summary 661 - Image Smoother

Directory link:

Likou Programming Questions - Solution Summary - Sharing + Recording - CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Original title link: force buckle


describe:

An image smoother is a 3 x 3 filter that smoothes each cell of an image, and the smoothed cell value is the average grayscale of that cell.

The average gray level of each cell is defined as the average of the cell itself and its surrounding 8 cells, and the result needs to be rounded down. (i.e. the average of the 9 cells in the blue smoother needs to be calculated).

If there are missing cells around a cell, the missing cells are not considered when calculating the average grayscale (ie, the average of 4 cells in the red smoother needs to be calculated).

Given an mxn integer matrix img representing the grayscale of the image, return the image smoothed for each cell of the image.

Example 1:

Input: img = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[0, 0, 0],[0, 0, 0], [0 , 0, 0]]
Explanation:
For points (0,0), (0,2), (2,0), (2,2): Average(3/4) = Average(0.75) = 0
for points ( 0,1), (1,0), (1,2), (2,1): average(5/6) = average(0.83333333) = 0
for point(1,1): average(8/9) = Average(0.88888889) = 0
Example 2:


Input: img = [[100,200,100],[200,50,200],[100,200,100]]
Output: [[137,141,137],[141,138,141],[137,141,137]]
Explanation:
For points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
for points (0,1), (1,0), (1, 2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
For point (1,1): floor((50+200+200 +200+200+100+100+100+100)/9) = floor(138.888889) = 138
 

hint:

m == img.length
n == img[i].length
1 <= m, n <= 200
0 <= img[i][j] <= 255

Source: LeetCode
Link: https://leetcode-cn.com/problems/image-smoother The
copyright belongs to LeetCode.com. For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.

Problem solving ideas:

* Problem-solving ideas: 
* This problem is not difficult, the focus is on how to write code more beautifully. 
* The idea here is to traverse each point to find the average grayscale, through the getSum method. 
* In the getSum method, traverse the x-axis to traverse x-1, x, x+1, and the Y-axis traverse y-1, y, y+1, a total of 9 points, if it exceeds the range, skip it. 
* so as to find the final gray value

Code:

public class Solution661 {

    public int[][] imageSmoother(int[][] img) {
        int[][] result = new int[img.length][img[0].length];
        for (int i = 0; i < img.length; i++) {
            int[] ints = img[i];
            for (int j = 0; j < ints.length; j++) {
                result[i][j] = getSum(0, j, i, img);
            }
        }
        return result;
    }

    private int getSum(int lastSum, int x, int y, int[][] img) {
        int sum = 0;
        int num = 0;
        for (int i = y - 1; i <= y + 1; i++) {
            for (int j = x - 1; j <= x + 1; j++) {
                if (i < 0 || j < 0 || i >= img.length || j >= img[0].length) {
                    continue;
                }
                sum += img[i][j];
                num++;
            }
        }
        return sum / num;
    }

}

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/123702118