LeetCode661. Image Smoother

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43274298/article/details/102748746

包含整数的二维矩阵 M 表示一个图片的灰度。你需要设计一个平滑器来让每一个单元的灰度成为平均灰度 (向下舍入) ,平均灰度的计算是周围的8个单元和它本身的值求平均,如果周围的单元格不足八个,则尽可能多的利用它们。

示例 1:

输入:
[[1,1,1],
[1,0,1],
[1,1,1]]
输出:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
解释:
对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
注意:

  1. 给定矩阵中的整数范围为 [0, 255]。
  2. 矩阵的长和宽的范围均为 [1, 150]。

方法一:8方向判断

class Solution {
    public int[][] imageSmoother(int[][] M) {
        int len1 = M.length;
        int len2 = M[0].length;
        int[][] nums = new int[len1][len2]; 
        for(int i = 0; i < len1; i++){
            for(int j = 0; j < len2; j++){
                int temp = 0;
                int count = 1;  
                temp += M[i][j];
                if(i-1 >= 0)
                {
                    temp += M[i-1][j]; 
                    count++;
                }
                if(i+1 < len1)
                {
                    temp += M[i+1][j];  
                    count++;
                }
                if(j-1 >= 0)
                {
                    temp += M[i][j-1];
                    count++;
                }
                if(j+1 < len2)
                {
                    temp += M[i][j+1];
                    count++;
                }
                if(i-1 >= 0 && j-1 >= 0)
                {
                    temp += M[i-1][j-1];
                    count++;
                }
                if(i-1 >= 0 && j+1 < len2)
                {
                    temp += M[i-1][j+1];
                    count++;
                }
                if(i+1 < len1 && j-1 >= 0)
                {
                    temp += M[i+1][j-1];
                    count++;
                }
                if(i+1 < len1 && j+1 < len2)
                {
                    temp += M[i+1][j+1];
                    count++;
                }
                nums[i][j]=temp/count;
            }
        }
        return nums;
    }
}

方法二:将所有邻居的和保存在 ans[r][c] 中,同时记录邻居的数目 count。最终的答案就是和除以邻居数目。

class Solution {
    public int[][] imageSmoother(int[][] M) {
        int R = M.length, C = M[0].length;
        int[][] ans = new int[R][C];

        for (int r = 0; r < R; ++r)
            for (int c = 0; c < C; ++c) {
                int count = 0;
                for (int nr = r-1; nr <= r+1; ++nr)
                    for (int nc = c-1; nc <= c+1; ++nc) {
                        if (0 <= nr && nr < R && 0 <= nc && nc < C) {
                            ans[r][c] += M[nr][nc];
                            count++;
                        }
                    }
                ans[r][c] /= count;
            }
        return ans;
    }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/image-smoother/solution/tu-pian-ping-hua-qi-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/qq_43274298/article/details/102748746