본문 바로가기
Problem Solving/LeetCode

[LeetCode] 661. Image Smoother - Java

by graycode 2026. 9. 19.

• 문제 링크

 

Image Smoother - LeetCode

Can you solve this real interview question? Image Smoother - An image smoother is a filter of the size 3 x 3 that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nin

leetcode.com

 

• 풀이 코드

public class Solution {

    public int[][] imageSmoother(int[][] img) {
        int n = img.length, m = img[0].length;
        int[][] mat = new int[n][m];
        for (int r = 0; r < n; r++) {
            for (int c = 0; c < m; c++) {
                int sum = 0, cnt = 0;
                for (int i = r - 1; i <= r + 1; i++) {
                    for (int j = c - 1; j <= c + 1; j++) {
                        if (i < 0 || i >= n || j < 0 || j >= m) continue;
                        sum += img[i][j];
                        cnt++;
                    }
                }
                mat[r][c] = sum / cnt;
            }
        }

        return mat;
    }

}

댓글