栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Leetcode每日一题:661. 图片平滑器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Leetcode每日一题:661. 图片平滑器

Leetcode每日一题:661. 图片平滑器

思路:

主要考察对矩阵的枚举编程功力。进行遍历,然后对每个位置进行计算结果。

每个位置的计算考虑行、列的下标及步长

如果下标大于0,起始下标步长会是3,否则起始位置为下标-=1,步长为2, 代码:

    
    public static int[][] imageSmoother(int[][] img) {
        int[][] res = new int[img.length][img[0].length];
        for (int row = 0; row < img.length; row++) {
            for (int column = 0; column < img[0].length; column++) {
                res[row][column] = count(img, row, column);
            }
        }
        return res;
    }

    
    private static int count(int[][] img, int row, int column) {
        int rowLen, columnLen;
        //对行和列确定起始遍历下标和步长:
        // 如果大于0,起始下标步长会是3,
        // 如果当前下标不大于0,起始位置为下标-=1,步长为2,
        if (row > 0) {
            row -= 1;
            rowLen = 3;
        } else {
            rowLen = 2;
        }
        if (column > 0) {
            column -= 1;
            columnLen = 3;
        } else {
            columnLen = 2;
        }
        int count = 0;
        int times = 0;
        //计算结果
        for (int i = row; i - row < rowLen && i < img.length; i++) {
            for (int j = column; j - column < columnLen && j < img[0].length; j++) {
                count += img[i][j];
                times++;
            }
        }
        return count / times;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780022.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号