每日一道Leetcode - 661. 图片平滑器【数组】

在这里插入图片描述

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

猜你喜欢

转载自blog.csdn.net/weixin_41041275/article/details/112554654
今日推荐