每日一练 - leetcode

 

 解题思路:此题的难点在于如何遍历要求的点的四周,如何实现,我们用了2个双重for循环解决了此问题,虽然逻辑简单,但是细节还有有很多

class Solution {
    public int[][] imageSmoother(int[][] img) {
        int R = img.length;
        int C = img[0].length;
        int[][] image = 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){
                            image[r][c] += img[nr][nc];
                            count++;
                        }
                    }
                }
                image[r][c] = image[r][c]/count;
            }
 
        }
        return image;
    }
}

  

执行用时: 10 ms
内存消耗: 39.5 MB
posted @ 2021-09-01 18:35  YBINing  阅读(31)  评论(0编辑  收藏  举报